javaScript Difference Between Two Dates in Years Months Days

javaScript Difference Between Two Dates in Years Months Days

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.

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

  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.

Leave a Reply

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