开发者

javascript date issue

开发者 https://www.devze.com 2023-04-12 02:18 出处:网络
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)

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开发者_C百科 to be October 3rd shoudn't I get a 3 when I call mydate.getDate();?

What I am I missing?


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


Try this instead

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消