JavaScript

JavaScript Date parse() Method

JavaScript Date.parse() method converts a date formatted string into returns the difference from the date within that string with 1st of January 1970 in milliseconds. This parse() method is used with the help of a dot operator with the Date object. To better understand this Date parse() method look at the syntax below.

When the Date.parse() is called, it is known as a Direct call to the parse() method. However, whenever an object of the Date is created using the new keyword and the Date() constructor, it is known as an implicit call to the parse() method

Syntax of Date.parse()

Date.parse(DateInString)

 
The following are the syntax details:

    • Date: The Date object of JavaScript
    • DateInString: The representation of Date in string format

Return Value: Difference of date with Jan 1st,1970 in milliseconds, or NaN if the string is invalid.

Additional Note: The parse() method was a feature of the ES1 release of JavaScript

Example 1: Date parse() Method With a Valid String

To demonstrate the working of the date parse() method, first create a string representing a specific date like

string = "20 July 2000"

 
After that, simply create a new variable and set it equal to date parse() method and pass in the string in the argument of the parse() method like

milli = Date.parse(string);

 
Afterwards, print the value from this “milli” variable using the console log function like:

console.log(milli);

 
Execute this code, and the output will be:


The output on the terminal is the number of milliseconds elapsed from 1970 to the date given in the string variable.

Example 2: Passing an Invalid String in the Date parse() Method

To demonstrate the return value of the Date parse() method with an invalid string, create a string with the following line:

string = "32 Feb 2005";

 
The above line is representing a date which is the 32nd of February 2002 which is invalid. Now we are going to pass this string into the Date parse() method and display the result on the terminal using the console log function:

console.log(Date.parse(string));

 
After executing, the following result is displayed on the terminal:


The result on the terminal is a NaN, which means that it is “Not a Number”.

Example 3: Calculating the Time Elapsed in Years From the Date parse() Method

To calculate the time elapsed since 1970 to the Date passed inside the parse() method can easily be calculated with the help of simple mathematical calculations. Write the following calculations for calculating years from milliseconds:

minutes = 1000 * 60;
hours = minutes * 60;
days = hours * 24;
years = days * 365;

 
Now, create a date string with the following line:

date_string = "25 Dec 2005";

 
Pass this string into the Date parse() method and store the outcome in a new variable:

milliseconds = Date.parse(date_string);

 
To convert the time elapsed, simply divide this millisecond variable by years and print the result onto the terminal using the console log function:

console.log(milliseconds/years);

 
Executing the program produces the following result:


This result is the number of years passed since 1st January 1970 till 25th Dec 2005.

Wrap-up

The Date parse() method is used to take a date_string and convert that string into the difference between the value represented by that string and 1st January 1970. This difference is represented in the form of milliseconds. The working method here is the parse() method (introduced in the ES1 release), which is used with the help of a dot operator with the Date object.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.