I'm trying to use JavaScript
's setMonth(...)
to add one month to current date. Everything seemed to work fine until today, the last day of the month. I'm seeing some odd behavior.
Here's the code:
var date = new Date();
console.log('Current Date', date);
var currentMonth = date.getMonth();
console.log('Current Month', currentMonth);
var nextMonth = parseInt(currentMonth) + 1
console.log('Next Month', nextMonth);
date.setMonth(nextMonth);
console.log('Next date month ID', date.getMonth())
console.log('Next Month Date', date);
And here is the output:
Current Date Wed May 31 2017 11:58:50 GMT-0700 (MST)
Current Month 4
Next Month 5
Next date month ID 6
Next Month Date Sat Jul 01 2017 11:58:50 GMT-0700 (MST)
Somehow, after setting the month, we are skipping June and going straight to July, even though we have hours left before June starts.
Here is JSFIDDLE