Using ==
does not work in general as it compares object identities not values.
Using the +
prefix or getTime()
to convert to milliseconds before comparison can also fail in the case your date objects have a time component and you only wish to compare their date portions. (Do not be tempted to reach for the toDate()
method as that is day of month, and discards month and year!).
This seems to be one of the few times that toDateString()
is useful, for a succinct solution without the use of additional libraries.
var date1=new Date("2014-12-10");
var date2=new Date("2014-12-10T01:00");
+date1 == +date2
>false
date1.getTime() == date2.getTime()
>false
date1.toDateString() == date2.toDateString()
>true