javascript difference between two dates in years months days

Today, We want to share with you javascript difference between two dates in years months days.In this post we will show you get all months between two dates in jquery, hear for javascript date difference in years, months, days we will give you demo and example for implement.In this post, we will learn about Difference between two dates in years, months, days in PHP with an example.

how to get all months between two dates in javascript?

javascript Code

today = new Date()
past = new Date(2010,05,01) // remember this is equivalent to 06 01 2010
//dates in js are counted from 0, so 05 is june

function calcDate(date1,date2) {
    var diff = Math.floor(date1.getTime() - date2.getTime());
    var day = 1000 * 60 * 60 * 24;

    var days = Math.floor(diff/day);
    var months = Math.floor(days/31);
    var years = Math.floor(months/12);

    var displayRes = date2.toDateString();
    displayRes += " was "
    displayRes += days + " days " 
    displayRes += months + " months "
    displayRes += years + " years ago \n"

    return displayRes
    }


a = calcDate(today,past)
console.log(a) // returns Tue Jun 01 2010 was 1143 days 36 months 3 years ago

Calculate the difference between two dates

index.html

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

javascript months between two dates

javascript code
javascript date difference in months

function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth();
    months += d2.getMonth();
    return months <= 0 ? 0 : months;
}

I hope you get an idea about moment js difference between two dates in years, months days.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.