
Stop Using new Date('2026-01-01') to Parse Date Strings
If you've been writing JavaScript for more than a week, you've probably written something like this: const d = new Date ( ' 2026-01-01 ' ); console . log ( d . toISOString ()); // "2026-01-01T00:00:00.000Z" ✓ (looks fine) And it looks fine. Until it doesn't. The Hidden Timezone Trap Here's where it breaks: const d = new Date ( ' 2026-01-01 ' ); console . log ( d . toLocaleDateString ( ' en-US ' )); // "12/31/2025" 😱 You passed in January 1st. You got back December 31st. This is not a bug — it's specified behavior. According to the ECMAScript spec, date-only strings in YYYY-MM-DD format are interpreted as UTC midnight . When you convert to local time in a UTC-5 timezone (US East Coast), UTC midnight becomes the previous day at 7pm. Compare this: // Date-only string → interpreted as UTC const d1 = new Date ( ' 2026-01-01 ' ); console . log ( d1 . toISOString ()); // "2026-01-01T00:00:00.000Z" // Date-time string (even just adding T00:00) → interpreted as LOCAL time const d2 = new Date (
Continue reading on Dev.to Tutorial
Opens in a new tab




