JavaScript difference between two dates in days hours and minutes

Find the difference between two dates in days, hours and minutes javascript. Through this tutorial, i am going to show you how to find difference between two dates and get the difference in days, hours, minutes.

Find the difference between two dates in the following

  • javascript difference between two dates in days
  • calculate the difference between two dates in hours javascript
  • javascript difference between two dates in minutes

1 – javascript difference between two dates in days

Use the following function to calculate the difference between 2 dates and get difference days; as shown below:

    function daysDiff(dt1, dt2) 
     {
      // calculate the time difference of two dates JavaScript
      var diffTime =(dt2.getTime() - dt1.getTime());
  
      // calculate the number of days between two dates javascript
      var daysDiff = diffTime / (1000 * 3600 * 24); 
      return daysDiff;
      
     }

See the following example to calculate the difference between 2 dates and get difference days; as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>calculate difference between two dates in days javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function daysDiff(dt1, dt2) 
     {
      // calculate the time difference of two dates JavaScript
      var diffTime =(dt2.getTime() - dt1.getTime());
  
      // calculate the number of days between two dates javascript
      var daysDiff = diffTime / (1000 * 3600 * 24); 
      return daysDiff;
      
     }
    dt1 = new Date("2019-11-27");
    dt2 = new Date("2019-11-29");
    var days = daysDiff(dt1, dt2)
    document.write( "calculate difference between two dates in days javascript :- " + days ); 
  </script>  
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>calculate difference between two dates in days javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function daysDiff(dt1, dt2) 
     {
      // calculate the time difference of two dates JavaScript
      var diffTime =(dt2.getTime() - dt1.getTime());
  
      // calculate the number of days between two dates javascript
      var daysDiff = diffTime / (1000 * 3600 * 24); 
      return daysDiff;
      
     }
    dt1 = new Date("2019-11-27");
    dt2 = new Date("2019-11-29");
    var days = daysDiff(dt1, dt2)
    document.write( "calculate difference between two dates in days javascript :- " + days ); 
  </script>  
</body>
</html>

2 – Calculate the difference between two dates in hours javascript

Use the following function to find difference between 2 dates and get a different hours; as shown below:

    function hoursDiff(dt1, dt2) 
     {
      // calculate the time difference of two dates JavaScript
      var diffTime =(dt2.getTime() - dt1.getTime());
  
      // calculate the number of days between hours dates javascript
      var hoursDiff = diffTime / (1000 * 3600); 
      return hoursDiff;
      
     }

Use the following example to find difference between 2 dates and get a different hours; as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>calculate difference between two dates in hours javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function hoursDiff(dt1, dt2) 
     {
      // calculate the time difference of two dates JavaScript
      var diffTime =(dt2.getTime() - dt1.getTime());
  
      // calculate the number of days between hours dates javascript
      var hoursDiff = diffTime / (1000 * 3600); 
      return hoursDiff;
      
     }
    dt1 = new Date("2019-11-27");
    dt2 = new Date("2019-11-29");
    var hours = hoursDiff(dt1, dt2)
    document.write( "calculate difference between two dates in hours javascript :- " + hours ); 
  </script>  
</body>
</html>

3 – javascript difference between two dates in minutes

Use the following function to find the difference between 2 dates and get difference minutes; as shown below:

    function minutesDiff(dt1, dt2) 
     {
      var diffMin =(dt2.getTime() - dt1.getTime()) / 1000;
      diff /= 60;
      return Math.abs(Math.round( diffMin ));
      
     }

Use the following example to find the difference between 2 dates and get difference minutes; as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>calculate difference between two dates in minutes javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function minutesDiff(dt1, dt2) 
     {
      var diffMin =(dt2.getTime() - dt1.getTime()) / 1000;
      diff /= 60;
      return Math.abs(Math.round( diffMin ));
      
     }
    dt1 = new Date("2019-11-27");
    dt2 = new Date("2019-11-28");
    var minutes = minutesDiff(dt1, dt2)
    document.write( "calculate difference between two dates in minutes javascript :- " + minutes ); 
  </script>  
</body>
</html>

Recommended JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.