JavaScript's Date Object and Its Time Zone Pitfalls
Time zone pitfalls in JavaScript's Date: it stores UTC internally, date-only strings parse oddly, server and client zones differ, and newer APIs help.
When handling dates and times in web frontend code, misunderstanding how the standard Date object actually behaves is an easy way to introduce time-zone-related bugs.
Date objects hold their value internally as UTC
A JavaScript Date object always holds its value internally as a UTC-based timestamp — milliseconds since January 1, 1970. Display-oriented methods like getHours() or toLocaleString(), on the other hand, return a value converted according to the local time zone setting of the environment the code is running in — usually the viewer's browser. Without understanding this "internal representation is UTC, but most method outputs are local" split, unintended conversions can slip in.
Watch out when parsing date-only strings
Passing a date-only string like "2026-08-06" to new Date() gets interpreted, in most implementations, as midnight UTC on that date. If the code is running in a time zone west of UTC, displaying that value directly in local time can shift the displayed date back by one day — a common and confusing bug. When you only need to work with a date, it's safer to explicitly specify a time and time zone, or to use a dedicated date-handling library.
Design around the server and client being in different time zones
A web application should always assume the server and the browser viewing it might be running in different time zones. Passing a server-generated datetime straight to the client and leaving its interpretation entirely to the client-side Date object's default behavior can cause the display to disagree between server and client. It's safer to exchange datetime data as UTC strings (ISO 8601 format, for example) and explicitly convert to local time on the client only right before display (see also "What Is ISO 8601 Datetime Format?" for details).
Modern API options
Recent JavaScript has been building out APIs designed to handle time zones more safely, including Intl.DateTimeFormat and the still-experimental Temporal API, both aimed at addressing the standard Date object's weak points. For new datetime handling code, it's worth considering these APIs instead.