JavaScript Date Comparisons Don’t Equal [duplicate]

When you use <= or >= to compare two date objects, they are compared via valueOf, which is the same as getTime for Date.

But when you use ==, they are two different objects of the same type, so it returns false.

Added some examples:

> new Date(2011, 7, 30, 0, 0, 0, 0) == new Date( 2011, 7, 30, 0, 0, 0, 0 )
false
> new Date(2011, 7, 30, 0, 0, 0, 0).getTime() == new Date( 2011, 7, 30, 0, 0, 0, 0).getTime()
true
> new Date(2011, 7, 30, 0, 0, 0, 0).valueOf() == new Date( 2011, 7, 30, 0, 0, 0, 0).valueOf()
true
> new Date(2011, 7, 30, 0, 0, 0, 0).valueOf() == new Date( 2011, 7, 30, 0, 0, 0, 0).getTime()
true

Leave a Comment