Solved

Week Ending Dates

Posted on 2004-10-15
13
234 Views
Last Modified: 2008-03-17
I wrote a little routine using VBScript that computes the current week ending date and one week in the future.  The script then populates a select box with the two dates....


<script language="vbscript">

     function additem(MyDateAdd)
           Set MyAdd = document.CreateElement("OPTION")
           MyAdd.Value = MyDateAdd
           MyAdd.Text = MyDateAdd
           document.MyForm.WkEndDate.add MyAdd
           Set MyAdd = Nothing
       end function
      
      DIM Dow, Cd, Rd, Od, Fd

      Cd = Date()
      Dow = weekday(Cd)
      
      
      if Dow > 4 then
            Rd = 7 - Dow
            Od = DateAdd("D",Rd,Cd)
            Fd = DateAdd("D",7,Od)
            additem(Od)
            additem(Fd)            
      else
            Rd = Dow
            Od = DateAdd("D",-Rd,Cd)
            Fd = DateAdd("D",7,Od)
            additem(Od)
            additem(Fd)
            
       end if

</script>

It works fine and is in use.  I want to know if anyone out there can duplicate this same routine in JavaScript.  Currently the page is only being used by people with MSIE but the more I use JavaScript for creating pages the better I like it but this little routine is way out of my league as my background is mostly Visual Basic 6 and some ASP.  Thanks in advance.

0
Comment
Question by:acaraciolo
  • 6
  • 2
  • 2
  • +2
13 Comments
 
LVL 15

Expert Comment

by:justinbillig
Comment Utility
BY week ending you mean work week, so monday through friday?
0
 
LVL 15

Expert Comment

by:justinbillig
Comment Utility
if its saturday or sunday do you go back to friday or forward to the next friday?
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
function additem(MyDateAdd)
{
   var newOption=new Option(MyDateAdd,MyDateAdd);
   document.MyForm.WkEndDate.add(newOption);
}
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
That the function to add a new option to the List. Can you explain on the weekend part please?
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
<script language="javascript">

   function additem(MyDateAdd)
   {
       var datestr = (MyDateAdd.getMonth()+1) + "/" + MyDateAdd.getDate() + "/" + MyDateAdd.getFullYear();
       var opts = document.MyForm.WkEndDate.options;
       opts.length++;
       opts[opts.length-1].value = datestr;
       opts[opts.length-1].text = datestr;
   }


  function onLoadHandler()
  {
     var Cd = new Date()
     var Dow = Cd.getDay();
     
     
     if (Dow > 4)
     {
          var Rd = 7 - Dow;
          Cd.setDate( Cd.getDate()+Rd );
          additem(Cd);
          Cd.setDate( Cd.getDate()+7 );
          additem(Cd);
     }
     else
     {
          var Rd = Dow;
          Cd.setDate( Cd.getDate()-Rd );
          additem(Cd);
          Cd.setDate( Cd.getDate()+7 );
          additem(Cd);
     }    

   }
</script>
</head>

<BODY onload="onLoadHandler();">
<FORM name="MyForm">
 <SELECT name="WkEndDate">
  <OPTION value="">Choose a Date</OPTION>
 </SELECT>
</FORM>
</BODY>
</HTML>
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
oops, I cut out the top two tags before the <script> tag:

<html>
<head>
<script ...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
Comment Utility
here is a shorter version:


<html>
<head>
<script language="javascript">

   function additem(MyDateAdd)
   {
       var datestr = (MyDateAdd.getMonth()+1) + "/" + MyDateAdd.getDate() + "/" + MyDateAdd.getFullYear();
       var opts = document.MyForm.WkEndDate.options;
       opts.length++;
       opts[opts.length-1].value = datestr;
       opts[opts.length-1].text = datestr;
   }


  function onLoadHandler()
  {
     var Cd = new Date()
     var Dow = Cd.getDay();
     
     var Rd = (Dow>4) ? 7-Dow : -Dow;    
     Cd.setDate( Cd.getDate()+Rd );
     additem(Cd);
     Cd.setDate( Cd.getDate()+7 );
     additem(Cd);
   }

</script>
</head>

<BODY onload="onLoadHandler();">
<FORM name="MyForm">
 <SELECT name="WkEndDate">
  <OPTION value="">Choose a Date</OPTION>
 </SELECT>
</FORM>
</BODY>
</HTML>
0
 
LVL 19

Expert Comment

by:dakyd
Comment Utility
If I'm not mistaken, knightEknight's script needs one slight modification.  VBscript's weekday() function returns 1 for sunday, 2 for monday, 3 for tuesday, etc.  Javascript's Date.getDay() function, however, returns 0 for sunday, 1 for monday, 2 for tuesday, etc.  So in order to preserve the logic of the original function, you need to change this line:
     var Dow = Cd.getDay();

to this:
     var Dow = Cd.getDay() + 1;

Now the two versions of the script should do the same thing.  Hope that helps.
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
good catch ... that may also have implications for the test:  Dow < 4
???
0
 
LVL 19

Expert Comment

by:dakyd
Comment Utility
I don't think you need to change anything else, because what you're adding to is the Date, not the Day (of the week).  And the Date is indexed the same in VBScript or Javascript.  The only thing we have to make sure is that we're adding the same amount (Rd) in both scripts.  Since we make sure that Rd only depends on Dow, if Dow is the same in both scripts (which is what we just finished fixing), then Rd should also be the same in both scripts.  Hope that helps.
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
yes, but 4 represents Wednesday for the 1-based Dow,
and Thursday for the 0-based Dow.
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
oh, ok ... I see what you are saying now.
0
 

Author Comment

by:acaraciolo
Comment Utility
Sorry for the delay working on 30 different projects at once (like who isn't).  Thanks,  the (Dow>4) ? 7-Dow : -Dow; made me stop and think, then DUH! I'm guessing that is the equivalent of an IIF statement in VB6.... and BTW Sunday is in fact the first day of the week (1) unless you indicate otherwise in the weekday() statement.  The script works great I replaced my VBScript with the Javascript one...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now