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
Written by Wei-Meng Lee
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Date
Authors
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
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#