Categories
JavaScript Answers

How to Check if Two JavaScript Dates Have the Same Date Info?

Spread the love

Sometimes, we want to check if two JavaScript dates have the same date info.

In this article, we’ll look at how to check if two JavaScript dates have the same date info.

Check if Two JavaScript Dates Have the Same Date Info

To check if two JavaScript dates have the same date info, we can use the valueOf or getTime method to return the timestamp value of both dates, which are numbers.

Then we can compare the returned timestamp numbers with === .

For instance, we can write:

const a = new Date(1995, 11, 17);  
const b = new Date(1995, 11, 17);  
console.log(a.getTime() === b.getTime())

Then the console log logs true since a and b have the same timestamp number.

Also, we can use valueOf by writing:

const a = new Date(1995, 11, 17);  
const b = new Date(1995, 11, 17);  
console.log(a.valueOf() === b.valueOf())

And we get the same result as the previous example.

Conclusion

To check if two JavaScript dates have the same date info, we can use the valueOf or getTime method to return the timestamp value of both dates, which are numbers.

Then we can compare the returned timestamp numbers with === .

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.