Why === fails when comparing dates in JavaScript
Intermediate · 4 min read
You make two dates for 15 January 2026 and compare them with ===. The answer is false. Then you try <= and get true, and >= also gives true. So the dates are neither less nor greater, yet not equal either. This trips up nearly everyone the first time, and it causes real bugs, like a "has the date changed?" check that always says yes.
In this tutorial you will compare two dates for the same moment with every operator, see which ones work, and learn why. Then you will know the right way to check two dates for equality. You need to know how to make a date with new Date(year, month, day) and that getTime() gives a date's timestamp in milliseconds.
Two kinds of comparison
JavaScript compares things in two different ways, depending on the operator.
=== and == with two objects ask one question: are these the same object? A date is an object. Two dates made by two separate new Date(...) calls are two objects, even if they hold the same moment. So === says false.
<, >, <= and >= work differently. They first turn both sides into numbers. A date turns into its timestamp, the number of milliseconds since 1 January 1970. Then they compare the numbers. Two dates for the same moment turn into the same number.
Objects and references in one example
Think of two printed copies of the same letter. They say the same thing, but they are two sheets of paper. === asks "is this the same sheet?":
const first = { day: 15 };
const second = { day: 15 };
console.log(first === second); // false
console.log(first === first); // true
Dates behave exactly like these objects with ===.
Here is the exercise. Try it yourself first if you like, then read on and we will work through it together.
29 Comparing dates: why === never works
fairTwo Date objects for the same instant. Show which comparison operators say they are equal and which do not, and explain the split.
Stuck? Three hints, smallest first
- 1.
===on two objects asks whether they are the same object, not whether they hold the same value. - 2. The relational operators convert to numbers first, which is why
<and>do work. - 3. So
a <= banda >= bcan both be true whilea === bis false.
Step 1: two dates, same moment
Make two dates for midnight on 15 January 2026, and one for the next day:
const a = new Date(2026, 0, 15);
const b = new Date(2026, 0, 15); // same instant, different object
const later = new Date(2026, 0, 16);
console.log(a === b); // false
console.log(a == b); // false
Both are false. == usually converts values before comparing, but when both sides are objects it doesn't convert anything. It checks whether they are the same object, just like ===.
Step 2: the order operators work
Now try the operators that convert to numbers:
console.log(a < later); // true
console.log(later > a); // true
console.log(a <= b); // true
console.log(a >= b); // true
These all compare timestamps, so they give the answers you expect. That is why sorting dates and checking "is this before that?" both work with plain operators.
It also leads to the strange-looking result from the start: a <= b and a >= b are both true, while a === b is false. The first two compare moments. The last compares objects.
Step 3: compare the timestamps
To ask "do these two dates hold the same moment?", turn both into numbers yourself and compare those:
console.log(a.getTime() === b.getTime()); // true
console.log(Number(a) === Number(b)); // true
console.log(+a === +b); // true
getTime(), Number(a) and +a all give the same timestamp. getTime() is the easiest to read. +a is the shortest: the unary plus turns its value into a number.
Step 4: subtraction gives the gap
Subtracting two dates also turns them into numbers, so it tells you how far apart they are in milliseconds:
console.log(b - a); // 0
console.log(later - a); // 86400000
Zero means the same moment. 86,400,000 milliseconds is one day.
Step 5: a helper you can reuse
Wrap the check in a function, so the intent is clear wherever you use it:
const sameInstant = (x, y) => x.getTime() === y.getTime();
console.log(sameInstant(a, b)); // true
console.log(sameInstant(a, later)); // false
Note that this checks the exact moment, down to the millisecond. If you want "same calendar day, any time", that is a different check that compares year, month and day.
The full solution
The panel builds the two dates and the later one, then checks ===, ==, the order operators, getTime(), Number and subtraction. Press Run to see each result.
const a = new Date(2026, 0, 15);
const b = new Date(2026, 0, 15); // same instant, different object
const later = new Date(2026, 0, 16);
console.log('a === b :', a === b, '<- object identity');
console.log('a <= b :', a <= b, ' a >= b:', a >= b, '<- numeric comparison');
console.log('a < later:', a < later);
console.log('b - a :', b - a, 'ms');
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('=== compares objects, so it is false', a === b, false);
test('and so is ==, for the same reason', a == b, false);
test('the timestamps are what to compare', a.getTime() === b.getTime(), true);
test('< and > convert to numbers, so they work', a < later, true);
test('and the other way round', later > a, true);
test('which leaves this: not less, not greater, and not ===', [a <= b, a >= b, a === b], [true, true, false]);
test('unary + is the shortest way to compare for equality', Number(a) === Number(b), true);
test('and subtraction gives zero for the same instant', b - a, 0);
test('the deep-equality helper agrees, because JSON goes through toISOString', same(a, b), true);
The rule is the difference between the two families of operator. === and == compare object references, so two dates are equal only if they are literally the same object. <, >, <= and >= convert both sides to numbers first, so they compare instants and behave sensibly.
That gives the situation in the sixth assertion, which is the one to remember: a <= b and a >= b are both true while a === b is false. For equality, compare getTime() — or +a === +b, which is the same thing more briefly. Sorting and range checks can use the operators directly.
Common mistakes
- Using
===to compare two dates. It checks whether they are the same object, so separate dates are never equal. - Switching to
==to fix it. With two objects,==also compares identity and still saysfalse. - Comparing
toString()output. It works for whole seconds, but it ignores milliseconds. ComparegetTime()instead.
Try it yourself
- Write
isBefore(x, y)andisAfter(x, y)using<and>. - Make two dates one millisecond apart. Print
x <= y,x >= yandsameInstant(x, y). - Use
indexOfto find a date in an array of dates. Why does it return-1? Fix it withfindIndexandgetTime().