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

getTime() Returns UTC Milliseconds

Discover that timestamps are always in UTC, and how this affects date arithmetic and comparisons.

getTime() Returns UTC Milliseconds

You'll learn: How getTime() always returns UTC milliseconds regardless of your timezone, making it reliable for date comparisons and arithmetic.

The Universal Timestamp

When you call getTime() on any Date object, it returns the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix epoch). Crucially, this value is always in UTC, no matter what timezone you're in or how you created the date.

Think of it like this: everyone around the world might display different local clock times, but we all agree on one universal "tick counter" that's been running since 1970. That's what getTime() gives you—the universal count.

Why This Matters

const date1 = new Date('2024-01-15T10:00:00Z');  // UTC time
const date2 = new Date('2024-01-15T10:00:00');   // Local time (varies by timezone)

console.log(date1.getTime());  // e.g., 1705316400000
console.log(date2.getTime());  // Different number if not in UTC timezone!

Even though both dates look similar, getTime() returns different values because they represent different absolute moments in time. The first is explicitly UTC (Z suffix), the second is interpreted as local time.

Safe Comparisons and Arithmetic

Because getTime() always returns UTC milliseconds, you can safely compare dates or calculate differences:

const start = new Date('2024-01-15T10:00:00');
const end = new Date('2024-01-15T14:30:00');

const diffMs = end.getTime() - start.getTime();  // Always accurate
const diffHours = diffMs / (1000 * 60 * 60);     // 4.5 hours

This works reliably because you're comparing universal timestamps, not timezone-dependent representations.

Key Takeaway: getTime() always returns milliseconds since epoch in UTC, making it the safest way to compare dates, calculate differences, or perform date arithmetic without timezone confusion.