javascript difference between two dates in hours, minutes and seconds

Get time difference between two dates in javascript. In this tutorial, we will explain how to calculate the difference between two dates and get the difference in hours, minutes and seconds.

When you work with javascript date, time, day methods. So you should also read this javascript date, time, day related posts:

Calculate time difference between two dates in JavaScript

Here are some examples to calculate time difference between in two dates:

  • Calculate Difference in Days
  • Calculate Difference in Hours
  • Calculate Difference in Minutes
  • Calculate Difference in Seconds

Calculate Difference in Days

Here, we will create a javascript that is used to calculate the difference between two dates in days.

Use the given below function, To calculate the difference between 2 dates and get difference days.

    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;

     }

Ex:-

<!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>

Result Of the above code is: 2 days

Calculate Difference in Hours

Next, we will create a javascript that is used to calculate the difference between two dates in hours.

Use the given below function, get the difference between 2 dates and get a different hours.

    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;

     }

Ex:-

<!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>

Result Of the above code is: 48 hours

Calculate Difference in Minutes

Now, we will create a javascript that is used to calculate the difference between two dates in minutes.

Use the given below function, to get the difference between 2 dates and get difference minutes.

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

     }

Ex:-

<!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>

Result Of the above code is: 1440 minutes

4. Calculate Difference in Seconds

If you want to create a similar function to calculate the difference in seconds between two dates, you can follow a comparable approach. Here’s a secondsDiff function:

function secondsDiff(dt1, dt2) {
    var diffTime = dt2.getTime() - dt1.getTime();
    var secondsDiff = diffTime / 1000;
    return secondsDiff;
}

// Example usage
const startDate = new Date('2023-01-01T12:00:00');
const endDate = new Date('2023-01-02T15:30:00');

const differenceInSeconds = secondsDiff(startDate, endDate);
console.log(`Difference in seconds: ${differenceInSeconds} seconds`);

Recommended Tutorials

  1. javascript Convert Hours to Minutes,Minutes to Seconds,date to Milliseconds
  2. Compare two dates with JavaScript – Examples
  3. JavaScript Get Month Name With Various Examples
  4. Get Month 2 Digits in JavaScript
  5. javaScript Get Current Year 2 and 4 Digit – Example
  6. Get Current Date Time javaScript
  7. JavaScript: Date setUTCDate() Method
  8. Set UTC Month In javaScript
  9. setUTCFullYear() Method JavaScript With Examples
  10. javascript date setUTCHours() Method With Examples
  11. JavaScript: d.setUTCMinutes() Method e.g.
  12. setUTCSecond() Method By JavaScript
  13. javaScript Date set UTC milliseconds
  14. setUTCSecond() Method By JavaScript
  15. JavaScript: Date.getUTCDate() Method
  16. getUTCmonth Method javaScript With Example
  17. Digital Clock with date in javaScript
  18. JavaScript: Set Date Methods
  19. JavaScript Get Date Methods

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *