setMonth must before setDate : (unsafe for months less than 31 days)
var dt = new Date(); dt.setFullYear(2014); dt.setMonth(11); dt.setDate(31);
And setMonth second parameter can also be used to set the date.
var dt = new Date(); dt.setFullYear(2014); dt.setMonth(11, 31);
If there are no arguments for the constructor, it will use the
current date and time. according to system settings.
Thus, using setMonth and setDate separately will still produce an unexpected result.
If the set values are greater than their logical range , the value will automatically be set to an adjacent value .
For example, if today 2014-09-30 , then
var dt = new Date(); dt.setFullYear(2014); dt.setMonth(1); dt.setDate(28);
To avoid this, set the values directly using the constructor.
var dt = new Date(2014, 11, 31);
xdazz Sep 09 '14 at 10:03 2014-09-09 10:03
source share