It is the definition of the Date object to use values 0-11 for the month field.
I believe that the constructor using a String is system-dependent (not to mention locale/timezone dependent) so you are probably better off using the constructor where you specify year/month/day as separate parameters.
BTW, in Firefox,
new Date("04/02/2008");
It works fine for me - it will interpret slashes, but not hyphens. This proves my point that using a String to construct a Date object is problematic. Use explicit values for month/day/year instead:
new Date(2008, 3, 2);
Answer from matt b on Stack OverflowIt is the definition of the Date object to use values 0-11 for the month field.
I believe that the constructor using a String is system-dependent (not to mention locale/timezone dependent) so you are probably better off using the constructor where you specify year/month/day as separate parameters.
BTW, in Firefox,
new Date("04/02/2008");
It works fine for me - it will interpret slashes, but not hyphens. This proves my point that using a String to construct a Date object is problematic. Use explicit values for month/day/year instead:
new Date(2008, 3, 2);
nice trick indeed, which I just found out the hard way (by thinking through it). But I used a more natural date string with a hyphen :-)
var myDateArray = "2008-03-02".split("-");
var theDate = new Date(myDateArray[0],myDateArray[1]-1,myDateArray[2]);
alert(theDate);
Be careful when falling back to Date constructor
How to REALLY get a UTC Date object
var date = new Date('2015-10-11T14:03:19.243Z'); var str = date.toISOString();
More on reddit.com