Last Updated: February 25, 2016
· weimeng

Comparing date objects in JavaScript using the == operator

Today I learnt that you can't compare two Date objects directly in JavaScript.

Using the default JavaScript Date object, you need to first reset the time in the date to zero, down to the milliseconds:

dateA.setHour(0,0,0,0) == dateB.setHour(0,0,0,0)


If using Moment.js, you can use the .diff method:

dateA.diff(dateB, 'days') == 0