How to compare times using moment.js and JavaScript?

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.