Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Week Ending Dates

Posted on 2004-10-15
13
240 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tiny glitch in my main slider 3 38
Add or delete table rows 10 53
CORS Error from Chrome, but FireFox is OK 1 79
Implement a function similar to grep in vanilla Javascript 3 18
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

809 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