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 millisecs = Date.parse ( "2016-04-08T15:48:22.000Z" );
console.log ( (new Date( millisecs )).getTime ( ) === millisecs ); // true

The format of date_time_string is of this form “yyyy-mm-ddThh:mm:ss.sssZ”.

It 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 01 (which is January) and date default to 01, 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 format “yyyy-mm-ddThh:mm:ss.sssZ” including the time zone, the result is highly 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

ES5 §15.9#sec-15.9.4.2

Date Topic

  1. JavaScript: Date Object and Properties
  2. JavaScript: Date.prototype and Properties
  3. JavaScript Date.parse()
  4. JavaScript: Date() Constructor
  5. JavaScript: Compute Date Range

Method Reference

  1. JavaScript: Array Methods
  2. JavaScript: String.prototype and Properties
  3. JavaScript: String Methods for Regex
  4. JavaScript: RegExp Object
  5. JavaScript: Regex Syntax
  6. JavaScript: Date Object and Properties
  7. JavaScript: JSON Object and Properties
  8. JavaScript: Math Object and Properties
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.