JavaScript Difference Between Two Dates in Seconds Example


Share:
Published By: Admin, Published On: Aug 26, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use javascript difference between two dates in seconds example. We will use how to use if difference between two dates in seconds javascript. Here you will learn how to use javascript difference between two dates in seconds. This is a short guide on difference between two dates in seconds. Let's get started with how to get difference between two dates in seconds in javascript.

Here i will give you many example how you can check javascript difference between two dates in seconds.

Example : 1
<html>
    <head>
        <title>JavaScript Difference Between Two Dates in Seconds - MyWebtuts.com </title>
    </head>
    <body>
        <script>
            var date1, date2;
            date1 = new Date();
            document.write(""+date1);
            date2 = new Date( "Dec 10, 2015 20:15:10" );
            document.write("<br>"+date2);
            // get total seconds between two dates
            var seconds = Math.abs(date1 - date2) / 1000;
            document.write("<br>Difference between 2 dates: "+seconds);
        </script>
    </body>
</html>
Output :
Tue Jul 13 2021 15:31:53 GMT+0530 (India Standard Time)
Thu Dec 10 2015 20:15:10 GMT+0530 (India Standard Time)
Difference between 2 dates: 176411803.999
Example : 2
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JavaScript Difference Between Two Dates in Seconds - MyWebtuts.com </title>
    </head>
    <body>
        <script type="text/javascript">
            let d1 = Date.now();
            let d2 = new Date(2021,3,2).getTime();
            console.log(d1);
            console.log(d2); 
        </script>
    </body>
</html>
Output :
1626170516508
1617301800000

It will help you....

Share: