In this javascript Tutorial, Calculate the difference between two dates in years months and days javascript. Here, we will explain how to calculate the difference between two dates and get the difference in years months days.

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

- JavaScript Get Month Name With Various Examples
- JavaScript Date Get Month 2 Digits By Examples
- javaScript Get Current Year 2 and 4 Digit – Example

## Calculate the difference between two dates in the following

- javascript difference between two dates in the year
- calculate the difference between two dates in month javascript
- javascript difference between two dates in days
- javascript difference between two dates in years months days

### 1. javascript difference between two dates in year

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

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

function yearDiff(dt1, dt2) { var diffYear =(dt2.getTime() - dt1.getTime()) / 1000; diffYear /= (60 * 60 * 24); return Math.abs(Math.round(diffYear/365.25)); }

#### Ex:-

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

Result Of the above code is: 1 Year

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

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

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

function monthDiff(dt1, dt2) { var diffMonth =(dt2.getTime() - dt1.getTime()) / 1000; diffMonth /= (60 * 60 * 24 * 7 * 4); return Math.abs(Math.round(diff)); }

#### Ex:-

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

Result Of the above code is: 5 Month

### 3. 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

### 4. javascript difference between two dates in years months days

javascript difference between two dates in years months days. Here we will create a new function, which is used to calculate the difference between two dates in years, months and days.

Using the below-given function, we will calculate the difference between two dates and get years, months and days between two dates:

function diff_year_month_day(dt1, dt2) { var time =(dt2.getTime() - dt1.getTime()) / 1000; var year = Math.abs(Math.round((time/(60 * 60 * 24))/365.25)); var month = Math.abs(Math.round(time/(60 * 60 * 24 * 7 * 4))); var days = Math.abs(Math.round(time/(3600 * 24))); return "Year :- " + year + " Month :- " + month + " Days :-" + days; }

#### Ex:-

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>javascript difference between two dates in years months days</title> </head> <body> <script type = "text/javascript"> function diff_year_month_day(dt1, dt2) { var time =(dt2.getTime() - dt1.getTime()) / 1000; var year = Math.abs(Math.round((time/(60 * 60 * 24))/365.25)); var month = Math.abs(Math.round(time/(60 * 60 * 24 * 7 * 4))); var days = Math.abs(Math.round(time/(3600 * 24))); return "Year :- " + year + " Month :- " + month + " Days :-" + days; } dt1 = new Date("2019-11-27"); dt2 = new Date("2018-06-28"); var diff_year_month_day = diff_year_month_day(dt1, dt2) document.write( "javascript difference between two dates in years months days :- " + diff_year_month_day ); </script> </body> </html>

Result of above code is: Year :- 1 Month :- 18 Days :-517

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.