Why Dates Are Hard: Local vs UTC
You'll learn why mixing local time and UTC causes some of the most frustrating date bugs in JavaScript.
The Core Problem
JavaScript's Date object operates in two different time zones simultaneously: your local time zone and UTC (Coordinated Universal Time, the global standard). Many methods come in pairs—one for local, one for UTC—and mixing them creates subtle bugs that are hard to catch.
Think of it like this: imagine a clock that shows both New York time and London time. If you set the hour using the New York dial but read it using the London dial, you'll get the wrong answer. That's exactly what happens when you mix local and UTC date methods.
Common Mixing Mistakes
const date = new Date('2024-06-15'); // Parsed as UTC midnight
console.log(date.getDate()); // Might show 14 or 15 depending on YOUR timezone!
// Creating in local time but displaying as UTC:
const local = new Date(2024, 5, 15); // Local June 15
console.log(local.toISOString()); // Shows UTC, could be June 14 or 16!
The bug: the date string is interpreted as UTC, but getDate() returns the local date. If you're in a timezone behind UTC (like US timezones), you'll see the previous day.
Why This Matters
These bugs are invisible during development (works in your timezone!) but break for users elsewhere. Dates get shifted by a day, appointments appear at wrong times, and data gets corrupted—all because methods from different time zones were mixed.
Key Takeaway: Always decide upfront whether you're working in local time or UTC, then stick to that family of methods consistently. Never mix getDate() with toISOString(), or Date.UTC() with local constructors.