Skip to content Skip to sidebar Skip to footer

New Date() Return A Day Before The Day In Date Time String In Javascript

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

Solution 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

newDate('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.

Solution 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

Post a Comment for "New Date() Return A Day Before The Day In Date Time String In Javascript"