Date.parse()

The Date.parse() method accepts a string argument representing a date.

The following date formats are supported:

FormatExample
month/date/year7/24/2012
month_name date, yearJanuary 31, 2021
day_of_week month_name date year hours:minutes:seconds time_zoneTue May 27 2012 12:34:56 GMT-0400
ISO 8601 extended format YYYY-MM-DDTHH:mm:ss.sssZ2012-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>
  
Click to view the demo

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>
  
Click to view the demo

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>
  
Click to view the demo
Home 
  JavaScript Book 
    Essential Types  

Date:
  1. Date Type
  2. Date getter
  3. Date Setter
  4. Date.parse()
  5. Date.UTC()
  6. Date.now()
  7. Date toLocaleString()
  8. Date toString()
  9. Date valueOf()
  10. Date toDateString()
  11. Date toTimeString()
  12. Date toLocaleDateString()
  13. Date toLocaleTimeString()
  14. Date toUTCString()