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

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

- javaScript Get Current Date Time
- Get UTC Hours, Minutes, Seconds, MilliSeconds javascript
- javascript date setUTCHours() Method With Examples

## Calculate 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

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

### 2. Calculate the difference between two dates in hours javascript

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

### 3. javascript difference between two dates 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

If you want to know more about javascript date and time methods, you may like following date and time methods:

## You may like

- javascript Convert Hours to Minutes,Minutes to Seconds,date to Milliseconds
- Compare two dates with JavaScript – Examples
- JavaScript Get Month Name With Various Examples
- Get Month 2 Digits in JavaScript
- javaScript Get Current Year 2 and 4 Digit – Example
- Get Current Date Time javaScript
- JavaScript: Date setUTCDate() Method
- Set UTC Month In javaScript
- setUTCFullYear() Method JavaScript With Examples
- javascript date setUTCHours() Method With Examples
- JavaScript: d.setUTCMinutes() Method e.g.
- setUTCSecond() Method By JavaScript
- javaScript Date set UTC milliseconds
- setUTCSecond() Method By JavaScript
- JavaScript: Date.getUTCDate() Method
- getUTCmonth Method javaScript With Example
- Digital Clock with date in javaScript
- JavaScript: Set Date Methods
- JavaScript Get Date Methods

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