JavaScript Date.parse

By Xah Lee. Date: . Last updated: .

Date.parse(date_time_string)

Parses date_time_string, returns the millisecond representation of that date, or NaN if the string is not in a correct date format.

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

let ms = Date.parse ( "2016-04-08T15:48:22.000Z" );

console.log ( ms ); // 1460130502000

console.log ( (new Date( ms )).getTime ( ) === ms ); // true

Format of Argument

The format of date_time_string is of this form:

yyyy-mm-ddThh:mm:ss.sssZ

date_time_string 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, ES5.1 spec says it should assume Z. ES5 §15.9#sec-15.9.1.15, while ES6 says it should assume local time. ECMAScript 2015 §Numbers and Dates#sec-date-time-string-format

As of , , , (new Date("2014-12-31T23:08")).getHours(), Firefox returns 23 (which is assuming local time), Chrome assumes Z and returns 15 or other value.

let 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

Reference

ECMAScript 2015 §Numbers and Dates#sec-date.parse

Date Topic

  1. JS: Date Tutorial
  2. JavaScript Date.parse
  3. JS: Compute Date Range

  1. JS: Date Object
  2. JS: Date Constructor
  3. JS: Date.prototype

JS Object Reference

  1. Global
  2. Object
  3. Array
  4. Function
  5. String
  6. RegExp
  7. Date
  8. Set
  9. Map
  10. JSON
  11. Math
  12. Reflect
  13. Number
  14. Boolean
  15. Symbol
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth