Course contentsShow
← JavaScript
Lesson 1641 of 2,72335. Standard Library and Built-insFree lesson

Comparing Dates Across Timezones

Master safe date comparison by converting to timestamps or UTC, avoiding local time comparison pitfalls.

Comparing Dates Across Timezones

What you'll learn: How to safely compare dates from different timezones by converting to timestamps or UTC, avoiding misleading local time comparisons.

The Problem with Local Comparisons

When you compare two Date objects, JavaScript compares their underlying UTC milliseconds (from getTime()). This seems safe, but the trap emerges when you create dates using local time methods or strings without explicit timezone info. You might think "March 15th at 3 PM" in New York equals "March 15th at 3 PM" in Tokyo, but those are actually different moments in time.

The Safe Approach: Use Timestamps

The most reliable way to compare dates is to use getTime(), which always returns UTC milliseconds since epoch:

const dateNY = new Date('2024-03-15T15:00:00-04:00'); // 3 PM Eastern
const dateTokyo = new Date('2024-03-16T04:00:00+09:00'); // 4 AM Tokyo (same moment!)

console.log(dateNY.getTime() === dateTokyo.getTime()); // true - same instant
console.log(dateNY < dateTokyo); // false - comparison uses timestamps automatically

Alternative: Compare in UTC

You can also compare using UTC methods to ensure you're comparing apples to apples:

const date1 = new Date('2024-03-15T15:00:00-04:00');
const date2 = new Date('2024-03-15T15:00:00+01:00');

// Compare UTC components
const sameUTCHour = date1.getUTCHours() === date2.getUTCHours(); // false - different moments

Key Takeaway: Always compare dates using their UTC timestamps (getTime()) or UTC methods (getUTCDate(), etc.) to avoid timezone confusion. Direct date comparison (<, >, ===) is safe because it uses timestamps, but be certain your dates represent the correct moments in time when created.