469,276 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,276 developers. It's quick & easy.

Calculating a date range

Claus Mygind
571 512MB
My date calculation do not seem to work out. I have a list box with a character string value of dates ie: 03/28/2009. I want to create a date range for the week. So I would like to get the starting and ending date for this week. In the code below is what I attempted, but the results I got were Tuesday Apr 28 and Wednesday Apr22. what am I doing wrong:

Expand|Select|Wrap|Line Numbers
  1.  
  2. var cW = document.getElementById('resetNonUnionTime').value; 
  3. var dEnd = new Date( cW.substr(6,4),cW.substr(0,2),cW.substr(3,2) ); 
  4. var dStart = new Date( dEnd );
  5. dStart.setDate(dStart.getDate()-6); 
  6. alert(cW+"\n"+dEnd+"\n"+dStart);
  7.  
Here is the actual results from the alert box:
03/28/2009
Tue Apr 28 2009 00:00:00 GMT-0500 (Central Daylight Time)
Wed Apr 22 2009 00:00:00 GMT-0500 (Central Daylight Time)
Apr 3 '09 #1
3 2469
Claus Mygind
571 512MB
Well it seemed to work when I removed the substr on the cW variable. But my question now is. Is this a reliable method of doing the date calculation? I am using Firefox. Will this method work with other browsers as well?

Expand|Select|Wrap|Line Numbers
  1.  
  2. var cW = document.getElementById('resetNonUnionTime').value; 
  3. var dEnd = new Date( cW ); 
  4. var dStart = new Date( cW ); 
  5. dStart.setDate(dStart.getDate()-6); 
  6. alert(cW+"\n"+dEnd+"\n"+dStart);
  7.  
Apr 3 '09 #2
acoder
16,027 Expert Mod 8TB
The month is 0-based, so just subtract 1 from it.
Apr 3 '09 #3
Claus Mygind
571 512MB
Thanks that explains it.
Apr 6 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by androtech | last post: by
12 posts views Thread by Anthony Robinson | last post: by
18 posts views Thread by dfetrow410 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.