A Date object’s getMonth() method seems to have a bug. Assuming the Date d is 2013-01-31, I attempt to set the month on d like this:
const d = new Date(); // 2013-01-31
d.setMonth(8);
console.log(d.getMonth());
The result is 9. Why? I tested this both in Chrome and Firefox.
I found out that when it’s the 31st, 30th, or 29th of a month, setting the date to a month that has a fewer number of days causes getMonth to return the wrong value.
Let’s break this down:
This is only an issue when the day value of
dis greater than the maximum number of days in the month passed tosetMonth(). Otherwise, it works as you’d expect.