new Date() return a day before the day in date time string in javascript

守給你的承諾、 提交于 2019-12-04 06:19:38

问题


I have an existing date time string in place

new Date('2014-08-01T00:00:00')

But instead of returning 2014-08-01, it returns as 2014-07-31 in the actually angularJS view.

I wonder is this date time string valid, if not, why its not valid.

Could the T be the reason that the string return a wrong date?

The console.log return a date of Thu Jul 31 2014 20:00:00 GMT-0400 (EDT)

Thank You

Lets call those -2 are toxic vote downs. They should really recall the days when they are struggling to understand the basic concepts that now apparant to them. Its a shame.


回答1:


At present (Autumn 2014), JavaScript's date/time format diverges from ISO-8601 in a very important way: If there's no timezone indicator on the string, it assumes Z ("Zulu", GMT).

So

new Date('2014-08-01T00:00:00')

...is August 1st at midnight GMT. If you live east of GMT, that will be on the 31st in your local time.

However, this incompatibility with ISO-8601 is being fixed in ES6 and some implementations (including the latest V8 in Chrome) are already updating it. The ES6 spec changes the default to local time; check out §20.3.1.15 ("Date Time String Format", the section number may change) in the draft PDFs or this unofficial HTML version.




回答2:


The displayed date uses the timezone of your browser/computer. This means that if you are in GMT-1 and you enter 2014-08-01T00:00:00, the actual date is 2014-08-01T00:00:00 - 1 hour = 2014-07-31T23:00:00



来源:https://stackoverflow.com/questions/26101197/new-date-return-a-day-before-the-day-in-date-time-string-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!