The Date.parse()
method accepts a
string argument representing a date.
The following date formats are supported:
Format | Example |
---|---|
month/date/year | 7/24/2012 |
month_name date, year | January 31, 2021 |
day_of_week month_name date year hours:minutes:seconds time_zone | Tue May 27 2012 12:34:56 GMT-0400 |
ISO 8601 extended format YYYY-MM-DDTHH:mm:ss.sssZ | 2012-0525T00:00:00 |
parse() |
Yes | Yes | Yes | Yes | Yes |
Date.parse(datestring);
Parameter | Description |
---|---|
datestring | Required. A string representing a date |
return a number, representing the number of milliseconds between the specified date time and midnight January 1, 1970.
The following code passes a string date value into the parse() function.
var aDate= new Date(Date.parse("May 21, 2012"));
console.log(aDate);
//Mon May 21 2012 00:00:00 GMT-0700 (Pacific Daylight Time)
aDate= new Date(Date.parse("asdf 21, 2012"));
console.log(aDate);//Invalid Date
If the string passed into Date.parse() doesn't represent a date,
then it returns NaN
.
Chrome returns a string:'Invalid Date'.
The code above generates the following result.
The Date constructor calls Date.parse() if a string is passed in
var aDate = new Date("May 25, 2004");
console.log(aDate);
//Tue May 25 2004 00:00:00 GMT-0700 (Pacific Daylight Time)
The code above generates the following result.