Date.parse()
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 |
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var aDate= new Date(Date.parse("May 21, 2012"));
document.writeln(aDate);//Mon May 21 2012 00:00:00 GMT-0700 (Pacific Daylight Time)
</script>
</head>
<body>
</body>
</html>
If the string passed into Date.parse() doesn't represent a date, then it returns NaN. Chrome returns a string:'Invalid Date'.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var aDate= new Date(Date.parse("asdf 21, 2012"));
document.writeln(aDate);//Invalid Date
</script>
</head>
<body>
</body>
</html>
The Date constructor calls Date.parse() if a string is passed in
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var aDate = new Date("May 25, 2004");
document.writeln(aDate);//Tue May 25 2004 00:00:00 GMT-0700 (Pacific Daylight Time)
</script>
</head>
<body>
</body>
</html>