Moment.js - Get difference in two birthdays in years, months and days
Above solution is not working with momentjs 2.19.1
Then with reference of Date of Joining calculation not working with moment 2.19.1 I have implemented new solution with latest solution for momentjs 2.19.1.
Required npm packages:
"moment": "^2.19.4",
"moment-duration-format": "^1.3.0",
"react-moment": "^0.6.8",
import following in reactjs:
import moment from "moment";
import Moment from "react-moment";
import momentDurationFormatSetup from "moment-duration-format";
var DOJ = moment([work_data.joining_date]).format("YYYY,MM,DD");
var today = moment().format("YYYY,MM,DD");
var totalMonths = parseInt(moment(today).diff(moment(DOJ), 'months'));
var totalDuration = moment.duration(totalMonths, "months").format();
if(totalDuration.search('1y') > -1) {
totalDuration = totalDuration.replace("1y", "1 Year,");
} else {
totalDuration = totalDuration.replace("y", " Years,");
}
if(totalDuration.search('1m') > -1){
totalDuration = totalDuration.replace("1m", "1 Month");
} else {
totalDuration = totalDuration.replace("m", " Months");
}
console.log(totalDuration);
I just converted @Josh Crozier's answer into a function and added hours, minutes and seconds too.
function diffYMDHMS(date1, date2) {
let years = date1.diff(date2, 'year');
date2.add(years, 'years');
let months = date1.diff(date2, 'months');
date2.add(months, 'months');
let days = date1.diff(date2, 'days');
date2.add(days, 'days');
let hours = date1.diff(date2, 'hours');
date2.add(hours, 'hours');
let minutes = date1.diff(date2, 'minutes');
date2.add(minutes, 'minutes');
let seconds = date1.diff(date2, 'seconds');
console.log(years + ' years ' + months + ' months ' + days + ' days ' + hours + '
hours ' + minutes + ' minutes ' + seconds + ' seconds');
return { years, months, days, hours, minutes, seconds};
}
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.
Example Here
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
I'm not aware of a better, built-in way to achieve this, but this method seems to work well.
Moment.js also has duration
object. A moment is defined as single point in time, whereas duration is defined as a length of time which is basically what you want.
var a = moment([2015, 11, 29]);
var b = moment([2007, 06, 27]);
var diffDuration = moment.duration(a.diff(b));
console.log(diffDuration.years()); // 8 years
console.log(diffDuration.months()); // 5 months
console.log(diffDuration.days()); // 2 days
What @Josh suggest may work, but is definitely not the right way of calculating difference in 2 moments.