JavaScript Date.parse()

Why Date.parse ?

The JavaScript Date.parse () method returns the time in milliseconds until the date we entered 1970. This method is a function built into javascript.

Syntax:

Date.parse(datestring)

  • datestring(required): The date value to be calculated.

Return:A Number, representing the number of milliseconds between the specified date-time and midnight January 1, 1970


JavaScript Date.parse Examples

Example 1

We’re entering two date values.
1.Date value: The date that JavaScript gets started. Returns 0.
2.Date value: I entered today’s date value. With the help of the Date.parse () method, I learned the last second from 1970 to today.

        let date1 = Date.parse('01 Jan 1970 00:00:00 GMT');
        console.log(date1);

        let date2 = Date.parse('27 Jan 2019 00:00:00 GMT');
        console.log(date2);

        let a = date2 - date1;
        console.log(a)

output:

0
1548547200000


Example 2

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

        let print = Math.round(date / years);
        console.log(print)
        

output:

42


Example 3

If the input string of date is not correct, it return NaN i.e, not a number.

        let date = "January 12, 2019 22:15 PM";

        var nowDate = Date.parse(date);
        console.log(nowDate);

output:

NaN


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started