Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
- A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).
parse() method takes a date string (such as
"Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the
setTime() method and the
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 +0430" (4 hours, 30 minutes east of the Greenwich meridian). If a time zone is not specified and the string is in an ISO format recognized by ES5, UTC is assumed. GMT and UTC are considered equivalent. The local time zone is used to interpret arguments in RFC2822 Section 3.3 format (or any format not recognized as ISO 8601 in ES5) that do not contain time zone information.
ECMAScript 5 ISO-8601 format support
The date time string may be in ISO 8601 format. For example,
"2011-10-10" (just date) or
"2011-10-10T14:48:00" (date and time) can be passed and parsed. The UTC time zone is used to interpret arguments in ISO 8601 format that do not contain time zone information (note that ECMAScript ed 6 draft specifies that date time strings without a time zone are to be treated as local, not UTC).
While time zone specifiers are used during date string parsing to interpret the argument, the value returned is always the number of milliseconds between January 1, 1970 00:00:00 UTC and the point in time represented by the argument.
Differences in assumed time zone
Given a date string of
"March 7, 2014",
parse() assumes a local time zone, but given an ISO format such as
"2014-03-07" it will assume a time zone of UTC. Therefore
Date objects produced using those strings will represent different moments in time unless the system is set with a local time zone of UTC. This means that two date strings that appear equivalent may result in two different values depending on the format of the string that is being converted (this behavior is changed in ECMAScript ed 6 so that both will be treated as local).
Fall-back to implementation-specific date formats
The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause
Date.parse() to return
However, invalid values in date strings not recognized as ISO format as defined by ES5 may or may not result in
NaN, depending on the browser and values provided, e.g.:
// Non-ISO string with invalid date values new Date('23/25/2014');
will be treated as a local date of 25 November, 2015 in Firefox 30 and an invalid date in Safari 7. However, if the string is recognized as an ISO format string and it contains invalid values, it will return
NaN in all browsers compliant with ES5:
// ISO string with invalid values new Date('2014-25-23').toISOString(); // returns "RangeError: invalid date" in all es5 compliant browsers
SpiderMonkey's implementation-specific heuristic can be found in
jsdate.cpp. The string
"10 06 2014" is an example of a non–conforming ISO format and thus falls back to a custom routine. See also this rough outline on how the parsing works.
new Date('10 06 2014');
will be treated as a local date of 6 October, 2014 and not 10 June, 2014. Other examples:
new Date('foo-bar 2014').toString(); // returns: "Invalid Date" Date.parse('foo-bar 2014'); // returns: NaN
IPOdate is an existing
Date object, it can be set to August 9, 1995 (local time) as follows:
IPOdate.setTime(Date.parse('Aug 9, 1995'));
Some other examples of parsing non–standard date strings:
Date.parse('Aug 9, 1995');
807937200000 in time zone GMT-0300, and other values in other time zones, since the string does not specify a time zone and is not ISO format, therefore the time zone defaults to local.
Date.parse('Wed, 09 Aug 1995 00:00:00 GMT');
807926400000 no matter the local time zone as GMT (UTC) is provided.
Date.parse('Wed, 09 Aug 1995 00:00:00');
807937200000 in time zone GMT-0300, and other values in other time zones, since there is no time zone specifier in the argument and it is not ISO format, so is treated as local.
Date.parse('Thu, 01 Jan 1970 00:00:00 GMT');
0 no matter the local time zone as a time zone GMT (UTC) is provided.
Date.parse('Thu, 01 Jan 1970 00:00:00');
14400000 in time zone GMT-0400, and other values in other time zones, since no time zone is provided and the string is not in ISO format, therfore the local time zone is used.
Date.parse('Thu, 01 Jan 1970 00:00:00 GMT-0400');
14400000 no matter the local time zone as a time zone GMT (UTC) is provided.
|ECMAScript 5.1 (ECMA-262)
The definition of 'Date.parse' in that specification.
|Standard||ISO 8601 format added.|
|ECMAScript 6 (ECMA-262)
The definition of 'Date.parse' in that specification.
|Feature||Chrome||Firefox (Gecko)||Internet Explorer||Opera||Safari|
|ISO 8601 format||(Yes)||4.0 (2.0)||9||(Yes)||(Yes)|
|Feature||Android||Chrome for Android||Firefox Mobile (Gecko)||IE Mobile||Opera Mobile||Safari Mobile|
|ISO 8601 format||?||(Yes)||(Yes)||?||(Yes)||(Yes)|