How to get the number of years since a date, not rounded up with Moment.js and JavaScript?

Sometimes, we want to get the number of years since a date, not rounded up with Moment.js and JavaScript.

In this article, we’ll look at how to get the number of years since a date, not rounded up with Moment.js and JavaScript.

How to get the number of years since a date, not rounded up with Moment.js and JavaScript?

To get the number of years since a date, not rounded up with Moment.js and JavaScript, we call the diff method.

For instance, we write

const years = moment().diff("1981-01-01", "years");

to get the number of years difference between today and January 1, 1981.

We call moment with nothing to get the current datetime as a moment object.

Then we call diff with the date string and 'years' to get the years difference.

Conclusion

To get the number of years since a date, not rounded up with Moment.js, we call the diff method.