Categories
JavaScript Answers

How to compare two times with Moment.js and JavaScript?

Spread the love

Sometimes, we want to compare two times with Moment.js and JavaScript.

In this article, we’ll look at how to compare two times with Moment.js and JavaScript.

How to compare two times with Moment.js and JavaScript?

To compare two times with Moment.js and JavaScript, we can parse times with moment.

For instance, we write

const beginningTime = moment("8:45am", "h:mma");
const endTime = moment("9:00am", "h:mma");
console.log(beginningTime.isBefore(endTime));

to call moment with the times we want to compare.

Then we check if beginningTime is before endTime with isBefore.

Conclusion

To compare two times with Moment.js and JavaScript, we can parse times with moment.

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 *