JavaScript Date parse() Method (original) (raw)

Last Updated : 12 Jul, 2024

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:

**Return Values:

**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);

`

**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);

`

**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: