2

When I'm working with date comparison in javascript, in the browser console I did perform the following operations.

new Date() >= new Date() returned true

new Date() <= new Date() returned true

this looked good as I thought both are equal, if I was correct then

new Date() == new Date() should returns true

Interestingly it returned false. Then I performed below operations as well

new Date() > new Date() returned false

new Date() < new Date() returned false

new Date() != new Date() returns true

which was also fine with my assumption.

If both the new Date() s were returning same time then == should return true along with the >= and <=, other wise either of > or < should return ture as != returning true.

Following table consists expected results and actual results for different cases.

enter image description here

Why is the ACTUAL results column not following any of its preceding columns?

Sivaprasad derangula
  • 1,169
  • 4
  • 12
  • 29
  • [How to determine equality for two JavaScript objects?](http://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects). Dates are probably not an exception. – Álvaro González Feb 06 '17 at 13:23
  • 1
    with those operators when you apply on Date object , you invoke to cast at integer ( similar with +new Date() that mean time in unix format - timestamp) – Vladu Ionut Feb 06 '17 at 13:23
  • 3
    When you are performing the `==` comparison, you are actually asking "are these variables referencing the same object", which is `false`. All other comparisons work on the timestamp within the objects. – Emil S. Jørgensen Feb 06 '17 at 13:24
  • forget about == case, what about remaining cases? if results are random, why results are consistent? – Sivaprasad derangula Feb 06 '17 at 13:31
  • 1
    The comparison operators (`<`, `>`, `<=`, `>=`) coerce Dates to their time value, whereas the equality operators (`==`, `===`, `!=`, `!==`) do not. In the second case you're comparing objects which are never equal. – RobG Feb 06 '17 at 20:29
  • you can also use `valueOf()` `d1.valueOf() == d2.valueOf()` where d1 and d2 are date objects – Sumit Joshi Jan 26 '18 at 10:04

2 Answers2

4

Use Date.getTime to compare the timestamp, otherwise you are simply comparing the objects, which we know aren't the same.

var d1 = new Date(),
  d2 = new Date();

function fullCompare(a, b) {
  console.log(a == b, a <= b, a >= b, a < b, a > b);
}
fullCompare(d1, d2);
fullCompare(d1.getTime(), d2.getTime());
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • okay, then. I have one doubt, while comparing as object, == is false, >= is true so > should return true right? – Sivaprasad derangula Feb 06 '17 at 13:35
  • 1
    @sitchie `>=` and `>` compares the inner timestamp as milliseconds since 1970, however and `>=` is `same value or higher than`, which returns true since both dates holds the same timestamp millisecond count. `>` on the other hand is `Higher than` comparison, which is false because they contain the **same** value. – Emil S. Jørgensen Feb 06 '17 at 13:41
3

Check for equality like you're doing compares the references to the objects. To compare the actual values of the objects themselves, the most common practice is to call getTime() method, which will return the number of milliseconds since 1970-01-01 00:00:00 UTC.

Therefore, the following code will return true:

(new Date()).getTime() == (new Date()).getTime()
rodrigocfd
  • 6,450
  • 6
  • 34
  • 68