Categories
JavaScript Answers

How to compare times using moment.js and JavaScript?

Spread the love

Sometimes, we want to compare times using moment.js and JavaScript.

In this article, we’ll look at how to compare times using moment.js and JavaScript.

How to compare times using moment.js and JavaScript?

To compare times using moment.js and JavaScript, we can compare the moment objects.

For instance, we write:

const past = moment().subtract("days", 1)
const now = moment()
console.log(past < now)

to create 2 moment date objects, past and now.

Then we can compare them directly with the usual JavaScript comparison operators.

The objects will automatically be converted to timestamps before comparison.

Therefore, the console log should log true.

Conclusion

To compare times using moment.js and JavaScript, we can compare the moment objects.

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 *