Can anyone explain the discrepancy in the output below? My code is as follows:
function addDaysToDate( theDate, days ) {
var thisDate = new Date( theDate.getTime() + days*24*60*60*1000 );
return new Date( thisDate );
}
var dt = new Date( '5/20/2014' );
for (i=0;i<6;i++) {
var dt = addDaysToDate( dt, 7 );
console.log( dt + '---' + dt.getMonth() + '/' + dt.getDate() + '/' + dt.getYear() );
}
The output generated from that is:
Tue May 27 00:00:00 PDT 2014---4/27/2014
Tue Jun 3 00:00:00 PDT 2014---5/3/2014
Tue Jun 10 00:00:00 PDT 2014---5/10/2014
Tue Jun 17 00:00:00 PDT 2014---5/17/2014
Tue Jun 24 00:00:00 PDT 2014---5/24/2014
Tue Jul 1 00:00:00 PDT 2014---6/1/2014
I have looked at this a number of times, and for the life of my I cannot rationalize why dt.getMonth() shows a month prior to the current month. I have confirmed identical behavior on both Chrome and IE.