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()

var 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.

var 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

var t = Date.parse("2011-09-02T05:29:26-07:00");
console.log((new Date(t)).toString());
// Fri Sep 02 2011 05:29:26 GMT-0700 (PDT)

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

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

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

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

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

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

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

var t = Date.parse("2 September, 2017");
console.log((new Date(t)).toString());
// Sat Sep 02 2017 00:00:00 GMT-0700 (PDT)

Reference

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

Date Topic

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

  1. JavaScript: Date Object
  2. JavaScript: Date Constructor
  3. JavaScript: 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
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.