curly{bits}

curlybits / Guides / Dates and time zones for developers: the rules that prevent bugs

Dates and time zones for developers: the rules that prevent bugs

Most date bugs come from one of five mistakes. Each one below is shown with code that was actually run.

The rules

  1. Store and send instants in UTC, as a Unix timestamp or an ISO 8601 string with Z or an offset.
  2. Convert to local time only when you display it, using the reader's time zone.
  3. Store the time zone name (like Asia/Bangkok), not just an offset, for anything that repeats or happens in the future.
  4. Never parse a date string without a time zone and assume you know what you got.
  5. Add days and months in the calendar, not as hours.

An instant vs a wall-clock time

An instant is a single moment everywhere on Earth: “the payment went through at 2026-11-13T02:00:00Z”. Store it in UTC. A wall-clock time is what a clock on the wall shows somewhere: “the shop opens at 09:00 in Bangkok”. That needs the time zone name to mean anything, because the same 09:00 is a different instant in London.

The same instant written in two time zones is still the same instant:

new Date('2026-11-13T09:00:00+07:00').toISOString()
// '2026-11-13T02:00:00.000Z'

Send ISO 8601 with a Z or an offset

2026-11-13T02:00:00Z and 2026-11-13T09:00:00+07:00 are unambiguous. 2026-11-13 09:00 isn't: the receiver has to guess the zone. JavaScript's JSON.stringify already writes dates in UTC:

JSON.stringify({ at: new Date(Date.UTC(2026, 10, 13, 2, 0)) })
// '{"at":"2026-11-13T02:00:00.000Z"}'

Unix timestamps work too, as long as everyone agrees on seconds or milliseconds. The Unix timestamp converter tells them apart and shows both.

JavaScript's parsing trap

These two strings look like the same day, but JavaScript reads them differently. On a computer in Bangkok:

new Date('2026-11-13').toISOString()        // '2026-11-13T00:00:00.000Z'  (read as UTC)
new Date('2026-11-13T00:00').toISOString()  // '2026-11-12T17:00:00.000Z'  (read as local time)

A date on its own is treated as UTC midnight, but a date with a time and no zone is treated as local time. So a birthday saved as 2026-11-13 can show up as 12 November for users west of London. If you mean a calendar date with no time, keep it as the string YYYY-MM-DD and don't turn it into a Date at all.

Also remember that JavaScript months start at 0: new Date(2026, 10, 13) is 13 November.

Daylight saving: a day isn't always 24 hours

New York moves its clocks back on 1 November 2026. Add 24 hours to 9:00 on 31 October and you land on 8:00:

// 31 Oct 2026, 09:00 in New York (UTC−4)
const start = new Date('2026-10-31T13:00:00Z');
const next = new Date(start.getTime() + 24 * 3600 * 1000);
// shown in America/New_York: 1 Nov 2026, 08:00 (UTC−5)

Python's aware datetimes do the opposite: adding timedelta(days=1) keeps the wall-clock time, so the real gap is 25 hours:

from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
a = datetime(2026, 10, 31, 9, 0, tzinfo=ZoneInfo("America/New_York"))
a + timedelta(days=1)   # 2026-11-01 09:00:00-05:00, which is 25 hours later

Neither is wrong; they answer different questions. For “same time tomorrow” (a daily reminder), add a calendar day in the user's time zone. For “exactly 24 hours later” (a token that expires), add hours to a UTC instant.

Daylight saving also creates times that happen twice and times that never happen. In London, 01:30 on 25 October 2026 occurs once in BST and again an hour later in GMT. In New York, nothing between 02:00 and 03:00 exists on 8 March 2026. Scheduled jobs set for those times run twice or not at all, which is why servers usually run cron in UTC (see the cron generator).

Store the zone name, not just the offset

An offset like +07:00 is a fact about one instant. A zone name like America/New_York is a set of rules, including when daylight saving starts and ends. If a user books a meeting for “9:00 every Monday in New York”, store the zone name; with only -04:00, every meeting after 1 November would be an hour off. Get the user's zone in the browser with:

Intl.DateTimeFormat().resolvedOptions().timeZone   // 'Asia/Bangkok'

Adding months

“One month after 31 January” has no right answer, and libraries disagree. Plain JavaScript rolls over:

const d = new Date('2026-01-31T00:00:00Z');
d.setUTCMonth(d.getUTCMonth() + 1);
d.toISOString()   // '2026-03-03T00:00:00.000Z'

Decide what your product needs (often “the last day of the next month”) and test the 29th, 30th and 31st.

Showing a time to each reader

Format at the last moment, in the reader's own zone and language:

new Date(1795000000 * 1000).toLocaleString('en-GB', { timeZone: 'Asia/Bangkok' })
// '18/11/2026, 18:06:40'

In chat apps you can let the app do it: a Discord timestamp shows every reader the time in their own zone.

In Python, avoid naive datetimes

datetime.utcfromtimestamp() returns a datetime with no zone attached, and it's deprecated since Python 3.12. Use an aware one:

from datetime import datetime, timezone
datetime.fromtimestamp(1795000000, tz=timezone.utc).isoformat()
# '2026-11-18T11:06:40+00:00'

How this guide was checked: every example on this page was run as written, using Node.js 22 and Python 3.13, on a machine set to Asia/Bangkok (UTC+7). Spotted a mistake? Tell us and we'll fix it.

Updated