Open In App

JavaScript Date parse() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The JavaScript Date parse() method parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970, UTC. If the argument is an invalid date string, it returns NaN (Not a Number).

Syntax:

Date.parse(datestring);

Parameters

This method accepts a single parameter:

  • datestring: This parameter holds the date as a string.

Return Values: It returns an integer value representing the number of milliseconds between midnight January 1, 1970, and the date provided. If by any means, the machine can’t recognize the string or the input string is invalid, it will return “NaN” instead of an integer.

JavaScript Date parse() Examples

Example 1: Parsing Date String to Milliseconds

The code takes a date string “February 18, 2018 12:30 PM” and parses it using the Date.parse() method, which returns the number of milliseconds since January 1, 1970. The result is logged to the console.

JavaScript
// Taking a date string as input.
let date = "February 18, 2018 12:30 PM";

// Calling parse function on input date string.
let msec = Date.parse(date);
console.log(msec);

Output
1518957000000

Example 2: Parsing Invalid Date String

The code attempts to parse an invalid date string “February 48, 2018 12:30 PM” using the Date.parse() method. Since the date is invalid, the method returns NaN (Not a Number), which is logged to the console.

JavaScript
// Taking wrong date string as input.
let date = "February 48, 2018 12:30 PM";

// calling parse function.
let msec = Date.parse(date);
console.log(msec);

Output
NaN

 Note: Once we get the millisecond count between two dates, we can easily find the number of hours, days, months, years, etc by simple maths calculation.

Supported Browsers:

The browsers supported by the JavaScript Date parse() method are listed below: 

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.


Last Updated : 14 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads