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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside