JavaScript date parse() method


The parse() method in JavaScript is used to parse the specified date string and returns the number of milliseconds between the specified date and January 1, 1970. If the string does not have valid values or if it is not recognized, then the method returns NaN.

The counting of milliseconds between two specified dates helps us to find the number of hours, days, months, etc. by doing easy calculations.

Syntax

Date.parse(datestring)

It contains a single parameter string that represents a date. This method returns a number that represents the number of milliseconds.

Let's see some illustrations of using the parse() method. In the first example, we are passing the valid date value, and in the second example, we are passing the invalid date value to see the result.

Example

Calculate the number of years between January 1, 1970 to March 21, 2012:

var d = Date.parse("March 21, 2012");
var minutes = 1000 * 60;
var hours = minutes * 60;
var days = hours * 24;
var years = days * 365;

var y = Math.round(d / years);

Example -