By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,259 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,259 IT Pros & Developers. It's quick & easy.

Calculating a date range

Claus Mygind
100+
P: 571
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
Share this Question
Share on Google+
3 Replies


Claus Mygind
100+
P: 571
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
Expert Mod 15k+
P: 16,027
The month is 0-based, so just subtract 1 from it.
Apr 3 '09 #3

Claus Mygind
100+
P: 571
Thanks that explains it.
Apr 6 '09 #4

Post your reply

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