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.

- 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

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));
}
```

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

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));
}
```

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

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;
}
```

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

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;
}
```

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

#javascript #programming

14.00 GEEK