Last Updated: February 25, 2016
·
8.179K
· 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)

Source: http://stackoverflow.com/questions/492994/compare-dates-with-javascript

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

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

Source: http://stackoverflow.com/a/7988730

Written by Wei-Meng Lee

Recommend
Say Thanks
Update Notifications Off
Respond