Link to home
Start Free TrialLog in
Avatar of acaraciolo
acaraciolo

asked on

Week Ending Dates

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.

Avatar of justinbillig
justinbillig

BY week ending you mean work week, so monday through friday?
if its saturday or sunday do you go back to friday or forward to the next friday?
function additem(MyDateAdd)
{
   var newOption=new Option(MyDateAdd,MyDateAdd);
   document.MyForm.WkEndDate.add(newOption);
}
That the function to add a new option to the List. Can you explain on the weekend part please?
Avatar of knightEknight
<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>
oops, I cut out the top two tags before the <script> tag:

<html>
<head>
<script ...
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
good catch ... that may also have implications for the test:  Dow < 4
???
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.
yes, but 4 represents Wednesday for the 1-based Dow,
and Thursday for the 0-based Dow.
oh, ok ... I see what you are saying now.
Avatar of acaraciolo

ASKER

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...