PDA

View Full Version : Manipulating Dates



gingerj
05-23-2006, 11:16 AM
Hi ya,

I'm having trouble working with the date object. I'm trying to add seven to the current date, but if I do say something like:



var forwardDate = currentDate+7;


That gives me the wrong date, if the date is greater than say 31, as then it won't be even a day in the calender.

Is there a way to make sure that if the date rolls past 31 it'll go down to the 1st or something?

I hope I make sense....

Twey
05-23-2006, 02:10 PM
You shouldn't add directly to the date, it'll cause odd behaviour. The Date object provides several methods used to set the current date; the easiest to use is the constructor function,
Date(/* String */ dateString)

mwinter
05-24-2006, 12:16 PM
I'm having trouble working with the date object. I'm trying to add seven to the current date [...]Use the setDate method:



var nextWeek = new Date();

nextWeek.setDate(nextWeek.getDate() + 7);


Is there a way to make sure that if the date rolls past 31 it'll go down to the 1st or something?Date rollover is automatic if you take the approach above. The same goes for the other set* methods.

Mike