chadgh
chadgh

Reputation: 9743

javascript date issue

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?

Upvotes: 2

Views: 954

Answers (3)

Hari Pachuveetil
Hari Pachuveetil

Reputation: 10374

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

Upvotes: 0

Mike Christensen
Mike Christensen

Reputation: 91580

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

Upvotes: 2

David Tran
David Tran

Reputation: 10606

Try this instead

mydate = new Date('2011/10/03');

Upvotes: 0

Related Questions