about javascript Date

The Date object looks simple. It has methods to get the date, month, year, hour,minute, second that it represents.  And it has methods to set these values. But it has no properties to access these values  directly. Things go pretty well until I look for a way to add a number of days to today’s date.

var day=new Date();
day.setDate(day.getDate()+5);

The default constructor of Date will create a Date object representing the current time. And I want to set the day to 5 days after today. The problem is if today is the near the end of current month, will it rewind to the end of the current month, or correctly proceed to the next month?  Note that the getDate() function always returns a number between 1-31. But the setDate() function allows a value beyond that range, and can deal with it correctly,i.e., adjust the month/year to proper value. So if today is near the end of a month or a year, the day will be set to a date of next month or next year.

Comments are closed, but trackbacks and pingbacks are open.