Sometimes, we want to get months difference with Moment.js and JavaScript.
In this article, we’ll look at how to get months difference with Moment.js and JavaScript.
How to get months difference with Moment.js and JavaScript?
To get months difference with Moment.js and JavaScript, we use the diff
method.
For instance, we write
const monthDiff = moment([2022, 1, 1]).diff(
moment([2013, 9, 31]),
"months",
true
);
to call moment
to convert the arrays with year, month, and day to moment objects.
Then we call diff
to get the difference in months between the 2 moment objects.
Conclusion
To get months difference with Moment.js and JavaScript, we use the diff
method.