JavaScript Difference Between Two Dates in Years Months Days

Calculate or find difference between two dates in years, months and days javaScript; Through this tutorial, i am going to show you how to calculate or find difference between two dates in years months and days javascript.

Calculate Difference Between Two Dates in Years Months Days in JavaScript

Use the following methods to find or calculate difference between two dates in years, months and days in JavaScript:

  • 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

Use following function to calculate difference between two dates in years in javascript; as shown below:

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

Using the following example for calculate difference between two dates in years in javascript; as shown below:

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

2 – Calculate the difference between two dates in Month javascript

Use function to find the difference between two dates in months in javascript; as shown below:

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

Use following example for find the difference between two dates in months in javascript; as shown below:

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

3 – JavaScript difference between two dates in days

Use following function to difference between two dates in years months and days in javascript; 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;
      
     }

Use following example to difference between two dates in years months and days in javascript; 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>

4 – javascript difference between two dates in years months days

Use the following function to difference between two dates in years months and days in javascript; as shown below:

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

Use the following example to difference between two dates in years months and days in javascript; as shown below:

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

Recommended JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.