Why Your Date Is Off by One Day (JavaScript Time Zones Explained)
helo from code
0:00 / 0:00
Why Your Date Is Off by One Day (JavaScript Time Zones Explained)
15 просмотров · 6 дней назад
helo from code
331 подписчик
15 просмотров · 6 дней назад
You pick March 10 in a date picker, and your app shows Saturday, March 9. It works on your machine and breaks for every user in the Americas. Here's exactly why, and how to fix it for good.
You'll learn:
• What a JavaScript Date really is (one number in UTC, no time zone inside)
• The parsing rule almost nobody knows: "2024-03-10" is UTC midnight, "2024-03-10T00:00" is local
• Why negative UTC offsets (the whole Western Hemisphere) see the previous day
• Why the spec works this way, and why it will never change
• Where the bug hides: input type="date", valueAsDate, API date columns
• The mirror bug east of London: toISOString().slice(0, 10)
• Daylight saving: 23- and 25-hour days, and why +86400000 breaks calendars
• The fix: instant vs calendar date, Intl, and Temporal.PlainDate
• How to test: TZ=America/Los_Angeles and Chrome DevTools Sensors
Chapters:
0:00 Intro
0:18 The bug: works in Europe, breaks in the US
0:47 What a Date really is
1:18 The parsing rule nobody knows
1:57 Why the day shifts
2:28 Why JavaScript does this
3:00 Where it hides: date inputs and APIs
3:30 The mirror bug: toISOString
3:56 Daylight saving: 25-hour days
4:25 The fix: instant vs calendar date
5:24 Temporal
5:53 Recap
All examples verified in Node with America/New_York, America/Los_Angeles, Europe/Moscow and UTC. References: MDN Date.parse, MDN HTMLInputElement.valueAsDate, MDN Temporal.PlainDate.
#javascript #webdevelopment #programming