Categories
JavaScript Answers

How to get date difference in number of days with Moment.js and JavaScript?

Spread the love

Sometimes, we want to get date difference in number of days with Moment.js and JavaScript.

In this article, we’ll look at how to get date difference in number of days with Moment.js and JavaScript.

How to get date difference in number of days with Moment.js and JavaScript?

To get date difference in number of days with Moment.js and JavaScript, we use the diff method.

For instance, we write

const a = moment([2022, 0, 29]);
const b = moment([2022, 0, 28]);
const diff = a.diff(b, "days");

to call a.diff with b and 'days' to get the difference between a and b in days.

Conclusion

To get date difference in number of days with Moment.js and JavaScript, we use the diff method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *