new Date() return a day before the day in date tim

2019-03-03 14:21发布

问题:

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