Solved

Week Ending Dates

Posted on 2004-10-15
13
235 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
ID: 12322713
BY week ending you mean work week, so monday through friday?
0
 
LVL 15

Expert Comment

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

Expert Comment

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

Expert Comment

by:surajguptha
ID: 12322737
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
ID: 12322921
<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
ID: 12322927
oops, I cut out the top two tags before the <script> tag:

<html>
<head>
<script ...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 12322956
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
ID: 12323879
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
ID: 12323896
good catch ... that may also have implications for the test:  Dow < 4
???
0
 
LVL 19

Expert Comment

by:dakyd
ID: 12324183
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
ID: 12337757
yes, but 4 represents Wednesday for the 1-based Dow,
and Thursday for the 0-based Dow.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 12337759
oh, ok ... I see what you are saying now.
0
 

Author Comment

by:acaraciolo
ID: 12340605
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

911 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

19 Experts available now in Live!

Get 1:1 Help Now