2

Why is it that in javascript I create a new date object mydate = new Date('2011-10-03'); and it prints as October 2nd? Sun Oct 02 2011 18:00:00 GMT-0600 (MDT)

If I set the date to be October 3rd shoudn't I get a 3 when I call mydate.getDate();?

What I am I missing?

chadgh
  • 9,063
  • 8
  • 38
  • 54

3 Answers3

2

I believe your date is off by one because it's being parsed in UTC time and you're displaying it in mountain time (I assume your local time). This is per ECMA spec.

See section 15.9.3.3 of the Javascript specification here:

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
0

Try this instead

mydate = new Date('2011/10/03');
vantrung -cuncon
  • 10,207
  • 5
  • 47
  • 62
  • That is great! But why is that? By simply changing the date format string it works like expected? – chadgh Oct 07 '11 at 22:28
0

I think it is setting the date to 2011-10-03 and the time to 00:00:01 for UTC.

And the print is converting that date object to your local time

Hari Pachuveetil
  • 10,294
  • 3
  • 45
  • 68