Moment.js – Get difference in two dates in years, months and days

Updated on     Kisan Patel

You could get the difference in years and add that to the initial date; then get the difference in months and add that to the initial date again.

In doing so, you can now easily get the difference in days and avoid using the modulo operator as well.

var a = moment([2015, 11, 29]);
var b = moment([2007, 06, 27]);
var years = a.diff(b, 'year');
b.add(years, 'years');
var months = a.diff(b, 'months');
b.add(months, 'months');
var days = a.diff(b, 'days');
console.log(years + ' years ' + months + ' months ' + days + ' days');
// 8 years 5 months 2 days


Leave a Reply