Sometimes, we want to get the difference between two dates in years, months, days in JavaScript.
In this article, we’ll look at how to get the difference between two dates in years, months, days in JavaScript.
How to get the difference between two dates in years, months, days in JavaScript?
To get the difference between two dates in years, months, days in JavaScript, we use the preciseDiff method.
For instance, we write
const starts = moment("2022-02-03 12:53:12");
const ends = moment();
const duration = moment.duration(ends.diff(starts));
const diff = moment.preciseDiff(starts, ends, true);
const diffHuman = moment.preciseDiff(starts, ends);
to get the duration between the starts and ends date with moment.duration and diff.
And then we call precisediff to get theg precise difference between the 2 dates.
Conclusion
To get the difference between two dates in years, months, days in JavaScript, we use the preciseDiff method.