Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does javascript interpret these identical dates differently

What's going on here:

> new Date('Apr 15 2013');
Mon Apr 15 2013 00:00:00 GMT+0100 (GMT Daylight Time)
> new Date('04/15/2013');
Mon Apr 15 2013 00:00:00 GMT+0100 (GMT Daylight Time)
> new Date('2013-04-15');
Mon Apr 15 2013 01:00:00 GMT+0100 (GMT Daylight Time)

Obviously, one is being interpreted as UTC time, while the other two are being interpreted as local time. What causes the difference in parsing?

like image 356
Eric Avatar asked Feb 20 '13 09:02

Eric


People also ask

How is Date stored in JavaScript?

JavaScript stores dates as number of milliseconds since January 01, 1970. Zero time is January 01, 1970 00:00:00 UTC. One day (24 hours) is 86 400 000 milliseconds.

How to get the time from new Date in JavaScript?

JavaScript Date getTime() getTime() returns the number of milliseconds since January 1, 1970 00:00:00.

How to extract Date and time from Date object in JavaScript?

If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.


3 Answers

From the specification:

The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

Of all the formats you provided, only '2013-04-15' is officially supported, so the others seem to fall back to implementation-dependent behavior.

like image 114
Felix Kling Avatar answered Oct 19 '22 18:10

Felix Kling


Your third example is the only one that is actually explained by the spec. When you call the Date constructor with a single argument, this is what happens (where v is the string passed to the constructor):

Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date.

The parse method will attempt to parse the string (emphasis added):

The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15).

If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

And the "Date Time String Format" is YYYY-MM-DDTHH:mm:ss.sssZ, and also defines a shorter version of the form YYYY-MM-DD, which is what you use in your third example.

For the other examples, the parse will fail and the behaviour is implementation-defined.

like image 41
James Allardice Avatar answered Oct 19 '22 19:10

James Allardice


The Date constructor delegates to Date.parse, and it appears that Date.parse has two variants:

Given a string representing a time, parse returns the time value. It accepts the RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g. "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.

Alternatively, the date/time string may be in ISO 8601 format. Starting with JavaScript 1.8.5 (Firefox 4), a subset of ISO 8601 is supported. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00 (date and time) can be passed and parsed.

Clearly, these behave differently with respect to local timezones


Edit: Looks like this is implementation defined

like image 35
Eric Avatar answered Oct 19 '22 20:10

Eric