148
rated 0 times
[
151]
[
3]
/ answers: 1 / hits: 36497
/ 9 Years ago, sun, november 29, 2015, 12:00:00
I'm looking for a way to use momentjs to parse two dates, to show the difference.
I want to have it on the format: X years, Y months, Z days.
For the years and months the moment library and modulo operator works nice. But for the days it's another tale as I don't want to handle leap years and all that by myself. So far the logic I have in my head is this:
var a = moment([2015, 11, 29]);
var b = moment([2007, 06, 27]);
var years = a.diff(b, 'years');
var months = a.diff(b, 'months') % 12;
var days = a.diff(b, 'days');
// Abit stuck here as leap years, and difference in number of days in months.
// And a.diff(b, 'days') returns total number of days.
return years + '' + months + '' + days;
More From » date