Categories
JavaScript Answers

How to compare dates with the isSame() function in moment.js and JavaScript?

Spread the love

Sometimes, we want to compare dates with the isSame() function in moment.js and JavaScript.

In this article, we’ll look at how to compare dates with the isSame() function in moment.js and JavaScript.

How to compare dates with the isSame() function in moment.js and JavaScript?

To compare dates with the isSame() function in moment.js and JavaScript, we call isSame with another moment object.

For instance, we write

const x = moment("28-02-2022", "DD-MM-YYYY");
const isSame = x.isSame(moment("28-02-2022", "DD-MM-YYYY"));

to call isSame with a moment object created with moment to check if it’s the same as x.

It should return true since x is the same as what we called isSame with.

Conclusion

To compare dates with the isSame() function in moment.js and JavaScript, we call isSame with another moment object.

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 *