Categories
JavaScript Answers

How to check if date between dates with Moment.js and JavaScript?

Spread the love

Sometimes, we want to check if date between dates with Moment.js and JavaScript.

In this article, we’ll look at how to check if date between dates with Moment.js and JavaScript.

How to check if date between dates with Moment.js and JavaScript?

To check if date between dates with Moment.js and JavaScript, we can use the isBetween method.

For instance, we write

const compareDate = moment("15/02/2022", "DD/MM/YYYY");
const startDate = moment("12/01/2022", "DD/MM/YYYY");
const endDate = moment("15/01/2022", "DD/MM/YYYY");
const isBetween = compareDate.isBetween(startDate, endDate, "days", "()");

to call compareDate.isBetween with startDate and endDate to check is compareDate is between startDate and endDate when comparing days.

The last argument is the inclusivity value, which specifies we compare exclusively.

We can also use '(]' for right inclusive, '[)' for left inclusive, or '[]' for all inclusive.

Conclusion

To check if date between dates with Moment.js and JavaScript, we can use the isBetween 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 *