0

I tried comparing 2 same dates in the chrome console:

new Date("2021-06-23") == new Date("2021-06-23")

It is giving false

new Date("2021-06-23") > new Date("2021-06-23")

It is giving false

But, new Date("2021-06-23") >= new Date("2021-06-23")

It is giving true

I couldn't understand why it is giving true for greater than or equal to but false for both greater than also and for equals to as well.

Please explain.

  • 1
    `==` when applied to objects checks if they are *the same object*, not if two different objects but with similar content. Relationship operators instead do implicit conversion. – VLAZ Jul 27 '21 at 18:36
  • 1
    ^ This. By contrast, `Date` objects have defined behaviour for `>/>=/<=` operators that compares the date value. – Niet the Dark Absol Jul 27 '21 at 18:36
  • I should also point out that since you aren't defining the time portion of the date, there's no guarantee that `new Date("2021-06-23") == new Date("2021-06-23")` would be true anyway, as the millisecond may change between calls. – Niet the Dark Absol Jul 27 '21 at 18:37
  • 2
    Does this answer your question? [JavaScript Date Object Comparison](https://stackoverflow.com/questions/7606798/javascript-date-object-comparison). Also see [JavaScript Date Comparisons Don't Equal](https://stackoverflow.com/questions/7244513/javascript-date-comparisons-dont-equal). – showdev Jul 27 '21 at 18:37
  • Try `new Date("2021-06-23").getTime() === new Date("2021-06-23").getTime();` the `getTime()` will get the `UNIX Timestamp` value. – Sajeeb Ahamed Jul 27 '21 at 18:38
  • you are comparing 2 objects, whatever their contains – Mister Jojo Jul 27 '21 at 18:40
  • 1
    @NiettheDarkAbsol if a ISO date string is passed then the milliseconds will be initialised to zero. – VLAZ Jul 27 '21 at 18:42

2 Answers2

0

The Date object don't support grater or equal operators per se... It has something like Symbol.toPrimitive

It's a function that gets called when it should be converted to the appropriated primitive value

const object1 = {
  [Symbol.toPrimitive](hint) {
    if (hint === 'number') {
      return 42;
    }
    return null;
  }
};

console.log(+object1);
// expected output: 42

So if you want to compare if two dates are equals then you can do

+new Date() === +new Date()

when you use any of this operators + - * / < > then it wishes to convert it to a number so you are actually comparing numbers and not objects

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Endless
  • 34,080
  • 13
  • 108
  • 131
  • A better reference is [ECMA-262 *Abstract Relational Comparison Algorithm*](https://262.ecma-international.org/#sec-abstract-relational-comparison), which uses the plain [*ToPrimitive*](https://262.ecma-international.org/#sec-toprimitive) abstract operation. [*Symbol.ToPrimitive*](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-symbol.toprimitive) is specifically for Symbol objects, not Date objects. – RobG Jul 28 '21 at 03:35
0

Because new Date create an unique object.

Even if they have the sames properties, 2 distinct objects will never been equals.

const x = {a : "aaa"};
const y = {a : "aaa"};

console.log(x == y); // false

const z = x; // litteraly the same object

console.log(x == z); // true
Maxime Girou
  • 1,511
  • 2
  • 16
  • 32