JS: Date.parse

By Xah Lee. Date: . Last updated: .
Date.parse(DateTimeStr)
Parses DateTimeStr, returns the millisecond representation of that date, or NaN if the string is not in a correct date format.

Date.parse(DateTimeStr) is equivalent to (new Date(DateTimeStr)).getTime()

const ms = Date.parse ( "2016-04-08T15:48:22.000Z" );
console.log( ms === 1460130502000);
console.log( (new Date( ms )).getTime() === ms );

Format of Date Time String

The format of DateTimeStr is of this form:

yyyy-mm-ddThh:mm:ss.sssZ

DateTimeStr is the same format returned by (new Date()).toISOString()

console.log( (new Date()).toISOString() ); // 2016-12-13T08:33:51.735Z

Trailing parts of the string can be omitted.

Example of valid argument:

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] As of 2022-07-28, chrome, Firefox, all assume local time.

const t = new Date(Date.parse("2016"));
console.log( t.toString() ); // Thu Dec 31 2015 16:00:00 GMT-0800 (PST)
console.log( t.getMonth() ); // 11
console.log( t.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" ))).toString() ===
 "Fri Sep 02 2011 05:29:26 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "2012-09-02" ))).toString() ===
 "Sat Sep 01 2012 17:00:00 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "09/02/2013" ))).toString() ===
 "Mon Sep 02 2013 00:00:00 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "9/02/2013" ))).toString() ===
 "Mon Sep 02 2013 00:00:00 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "9/2/2013" ))).toString() ===
 "Mon Sep 02 2013 00:00:00 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "Fri, 2 Sep 2014 11:14:11 +0200" ))).toString() ===
 "Tue Sep 02 2014 02:14:11 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "2 Sep, 2016" ))).toString() ===
 "Fri Sep 02 2016 00:00:00 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "Sep 2, 2015" ))).toString() ===
 "Wed Sep 02 2015 00:00:00 GMT-0700 (PDT)"
);
// true

console.log(
( new Date ( Date.parse ( "2 September, 2017" ))).toString() ===
 "Sat Sep 02 2017 00:00:00 GMT-0700 (PDT)"
);
// true
BUY ΣJS JavaScript in Depth