JavaScript Difference Between Two Dates In Minutes & Hours Example


Share:
Published By: Admin, Published On: Sep 07, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use javascript get difference between two dates in minutes and hours example. We will use how to use if get difference between two dates in minutes and hours in javascript. Here you will learn how to use javascript get difference between two dates in minutes and hours. Let's get started with how to get difference between two dates in minutes and hours in javascript.

Here i will give you many example how you can check javascript get difference between two dates in minutes and hours. Example : 1
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript difference between two dates in minutes and hours - MyWebtuts.com </title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>
    </head>
    <body>
    <script type="text/javascript"> 
        var startTime = moment('02-01-2021 01:01:01', 'DD-MM-YYYY hh:mm:ss');
        var endTime = moment('03-01-2021 16:52:53', 'DD-MM-YYYY hh:mm:ss');
      
        var hoursDiff = endTime.diff(startTime, 'hours');
        document.write('Hours:' + hoursDiff);
      
        var minutesDiff = endTime.diff(startTime, 'minutes');
        document.write('</br>Minutes:' + minutesDiff);
    </script>
    </body>
</html>
Output:
Hours:39
Minutes:2391
Example : 2
<html>
    <head>
        <title>JavaScript difference between two dates in minutes and hours - MyWebtuts.com </title>
    </head>
    <body>
    <script>  
        var var1, var2;  

        var1 = new Date( "Jan 25, 2021 11:10:05" );
        document.write(""+var1);
        var2 = new Date( "Feb 5, 2021 08:15:10" );
        document.write("<br>"+var2);

        var res = Math.abs(var1 - var2) / 1000;                       
        var hours = Math.floor(res / 3600) % 24;        
        document.write("<br>Difference (Hours): "+hours);  

        var minutes = Math.floor(res / 60) % 60;
        document.write("<br>Difference (Minutes): "+minutes); 
    </script>
    </body>
</html>
Output:
Mon Jan 25 2021 11:10:05 GMT+0530 (India Standard Time)
Fri Feb 05 2021 08:15:10 GMT+0530 (India Standard Time)
Difference (Hours): 21
Difference (Minutes): 5

It will help you....

Share: