difference in months between two dates in javascript

Solutions on MaxInterview for difference in months between two dates in javascript by the best coders in the world

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
pinned-register now
showing results for - "difference in months between two dates in javascript"
Joelle
09 Feb 2019
1function monthDiff(d1, d2) {
2    var months;
3    months = (d2.getFullYear() - d1.getFullYear()) * 12;
4    months -= d1.getMonth();
5    months += d2.getMonth();
6    return months <= 0 ? 0 : months;
7}
8