JS: Date.parse
Date.parse(ISO_string)-
- Parses ISO_string, return the millisecond representation of that date.
- Return NaN if the string is not in a correct date format.
console.assert(Date.parse("2016-04-08T15:48:22.000Z") === 1460130502000);
Format of Date Time String
The format of ISO_string is of this form:
yyyy-mm-ddThh:mm:ss.sssZ
- T is a literal letter T
- The “sss” is milliseconds
- “Z” stands for UTC (UTC is Coordinated Universal Time, practically the same as GMT (Greenwich Mean Time)). e.g.
"2016-04-08T15:48:22.000Z"
ISO_string is the same format returned by (new Date()).toISOString()
console.log((new Date()).toISOString()); // 2026-02-04T15:57:50.691Z
Trailing parts of the string can be omitted.
Example of valid argument:
"2016""2016-04""2016-04-08""2016-04-08T15""2016-04-08T15:48""2016-04-08T15:48:22""2016-04-08T15:48:22.000Z"
When trailing parts of the string are omitted, month default to January, and date default to 1st, Hour, minute, second, milliseconds defaults to 0.
For the time zone, JavaScript spec 2015 says it should assume local time. Before that, it's assume to be Z. 〔see JavaScript Spec Change on Date Time Zone Default (2022)〕 As of 2022-07-28, chrome, Firefox, all assume local time.
const xdate = new Date(Date.parse("2016")); console.log(xdate.toString()); // Thu Dec 31 2015 16:00:00 GMT-0800 (PST) console.log(xdate.getMonth()); // 11 console.log(xdate.getDate()); // 31
Testing Different Date String Formats
When the date string is not in the full format “yyyy-mm-ddThh:mm:ss.sssZ” including the time zone, the result is browser dependent.
// testing Date.parse with different formats of date string // warning: the result is browser dependent console.log(new Date(Date.parse("2011-09-02T05:29:26-07:00"))); // 2011-09-02T12:29:26.000Z console.log(new Date(Date.parse("2012-09-02"))); // 2012-09-02T00:00:00.000Z console.log(new Date(Date.parse("09/02/2013"))); // 2013-09-02T07:00:00.000Z console.log(new Date(Date.parse("9/02/2013"))); // 2013-09-02T07:00:00.000Z console.log(new Date(Date.parse("9/2/2013"))); // 2013-09-02T07:00:00.000Z console.log(new Date(Date.parse("Fri, 2 Sep 2014 11:14:11 +0200"))); // 2014-09-02T09:14:11.000Z console.log(new Date(Date.parse("2 Sep, 2016"))); // 2016-09-02T07:00:00.000Z console.log(new Date(Date.parse("Sep 2, 2015"))); // 2015-09-02T07:00:00.000Z console.log(new Date(Date.parse("2 September, 2017"))); // 2017-09-02T07:00:00.000Z