javaScript Difference Between Two Dates in Years Months Days

javaScript Difference Between Two Dates in Years Months Days

In this javascript tutorial, we will explain how to calculate the difference between two dates and get the difference in years, months and 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 Difference Between Two Dates in Years Months Days

  • Calculate Difference in Years
  • Calculate Difference in Months
  • Calculate Difference in Days
  • Calculate Difference in Years Months and Days

Calculate Difference in Years

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

Calculate Difference in Months

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

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

Calculate Difference in Years Months and Days

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

Using the below-given function, you can 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>

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

Recommended Tutorials

  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.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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