I have a date object and I need to create another date object that is 1 week after the first date object. I already have an implementation but it seems that there is a bug with javascript when it reaches the months of October, November and December. Is there a workaround for this? Note that the behavior is consistent across Chrome, FF and IE.
// ************ TEST#1 ************ var startDate = new Date(2011,08,05); // set to Sept 5, 2011 alert('START DATE' + startDate); var endDate = new Date(); endDate.setDate(startDate.getDate() + 7); alert('END DATE' + endDate); // endDate is Sept 12 which is correct // check that startDate's value is unchanged alert('START DATE' + startDate); // ************ TEST#2 ************ var startDate = new Date(2011,10,05); // set to Nov 5, 2011 alert('START DATE' + startDate); var endDate = new Date(); endDate.setDate(startDate.getDate() + 7); alert('END DATE' + endDate); // endDate is Sept 12, 2011 which is wrong alert('START DATE' + startDate); // ************ TEST#3 ************ // changed implementation but this won't work var startDate = new Date(2011,10,05); alert('START DATE' + startDate); var endDate = startDate; endDate.setDate(startDate.getDate() + 7); alert('END DATE' + endDate); // endDate is correct but... alert('START DATE' + startDate); // startDate's value has changed as well