Comparing Dates in JavaScript: How to Avoid Pitfalls and Speed Up Your Code
\n\nImagine: you're writing an online store, and you need to check if a product discount has expired. Or you're making a task calendar where you need to filter events for today. Or you're gathering analytics — "show all orders from the last week." Everywhere you need to compare two dates. It seems simple, right? But JavaScript, as always, is ready to spring surprises.
\n\nThe question "Compare two dates with JavaScript" on Stack Overflow has gathered thousands of views — and for good reason. Developers, even experienced ones, often stumble on non-obvious features of working with dates. In this article, we'll not only show how to compare two dates but also analyze which method is faster and which is safer. Let's go!
\n\nWhy doesn't simple date comparison work?
\n\nLet's start with a classic beginner's mistake. Suppose we have two dates:
\n\nlet date1 = new Date('2024-03-15');\nlet date2 = new Date('2024-03-20');\n\nconsole.log(date1 < date2); // true\nconsole.log(date1 > date2); // false\nconsole.log(date1 == date2); // false\n\n\nThe < and > operators work perfectly. JavaScript converts Date objects to numbers (milliseconds) and compares them. But the == operator (and strict ===) behaves differently. It compares object references, not their values. Two different dates are two different objects, so == will always return false, even if the dates match down to the millisecond.
This is the first trap. The moral: never use == or === to compare dates. Only use <, >, <=, >= or special methods.
The getTime() method: the gold standard for date comparison
\n\nThe most reliable and performant way is to use the getTime() method. It returns the number of milliseconds elapsed since January 1, 1970 (Unix epoch). And JavaScript is great at comparing numbers:
let date1 = new Date('2024-03-15T10:00:00');\nlet date2 = new Date('2024-03-15T10:00:00');\nlet date3 = new Date('2024-03-20T10:00:00');\n\nconsole.log(date1.getTime() === date2.getTime()); // true\nconsole.log(date1.getTime() < date3.getTime()); // true\nconsole.log(date1.getTime() > date3.getTime()); // false\n\n\nWhy is this efficient? Because getTime() is a very lightweight operation. It simply returns the numeric value already stored inside the Date object. No parsing, no extra calculations. Just a number.
Let's measure performance. Here's a quick test in the browser console:
\n\nlet date1 = new Date('2024-01-01');\nlet date2 = new Date('2024-12-31');\n\nconsole.time('getTime');\nfor (let i = 0; i < 1000000; i++) {\n date1.getTime() < date2.getTime();\n}\nconsole.timeEnd('getTime');\n// Result: ~5-10 ms in modern browsers\n\n\nA million comparisons in 10 milliseconds — that's an excellent result. For comparison, alternative methods can be 2-3 times slower.
\n\nComparing dates with valueOf()
\n\nThe valueOf() method does the same thing as getTime() — it returns the numeric representation of the date. But there's a nuance: when using comparison operators (<, >, etc.), JavaScript implicitly calls valueOf() for Date objects. That is, this code:
let date1 = new Date('2024-03-15');\nlet date2 = new Date('2024-03-20');\nconsole.log(date1 < date2);\n\n\nActually works like:
\n\nconsole.log(date1.valueOf() < date2.valueOf());\n\n\nSo for simple < and > comparisons, you can use Date objects directly. But for exact equality (===), this won't work, and you'll need getTime() or va