Comparing Dates in JavaScript: Optimization and Performance

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Comparing Dates in JavaScript: How to Avoid Pitfalls and Speed Up Your Code

\n\n

Imagine: 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\n

The 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\n

Why doesn't simple date comparison work?

\n\n

Let's start with a classic beginner's mistake. Suppose we have two dates:

\n\n
let 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\n

The < 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.

\n\n

This is the first trap. The moral: never use == or === to compare dates. Only use <, >, <=, >= or special methods.

\n\n

The getTime() method: the gold standard for date comparison

\n\n

The 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:

\n\n
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\n

Why 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.

\n\n

Let's measure performance. Here's a quick test in the browser console:

\n\n
let 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\n

A million comparisons in 10 milliseconds — that's an excellent result. For comparison, alternative methods can be 2-3 times slower.

\n\n

Comparing dates with valueOf()

\n\n

The 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:

\n\n
let date1 = new Date('2024-03-15');\nlet date2 = new Date('2024-03-20');\nconsole.log(date1 < date2);\n
\n\n

Actually works like:

\n\n
console.log(date1.valueOf() < date2.valueOf());\n
\n\n

So 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

Blogs

Book Recommendations