Categories
JavaScript Answers

How to Check if a Date is Between Two Dates with Moment.js?

Spread the love

We can use the isBetween method to check if a date is between 2 dates.

For instance, we can write:

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

We create the compareDate moment object that we want to check if it’s between startDate and endDate .

To do the comparison, we call isBetween on compareDate with startDate and endDate as the arguments.

Then isBetween is false since compareDate isn’t between startDate and endDate .

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 *