Dates in JSON: ISO 8601, Time Zones and Common Bugs
JSON has strings, numbers, booleans and null – but no date type. Every API has to pick a way to write dates as text or numbers, and most date bugs (“the date is one day off”, “the time is 5 hours wrong”) come from that choice. This guide shows the format to use and the traps in JavaScript and Python. The JavaScript outputs below were produced with the clock set to New York time (UTC−5), where the problems are easy to see.
The recommendation: ISO 8601 in UTC
{
"createdAt": "2026-11-20T10:00:00Z",
"birthday": "1990-04-15"
}- Moments in time (when something happened): full timestamp with a time zone.
Zmeans UTC; an offset like+02:00works too. - Calendar dates (birthdays, due dates, holidays): just
YYYY-MM-DD, with no time and no zone. - ISO strings sort correctly as plain text, are readable in logs, and every language can parse them.
Avoid local formats like "20/11/2026" or "11/20/2026": half the world reads them differently, and new Date("20/11/2026") returns Invalid Date.
Trap 1: date-only strings are UTC in JavaScript
new Date("2026-11-20").toISOString() // "2026-11-20T00:00:00.000Z"
new Date("2026-11-20").getDate() // 19 ← in New York!A date-only ISO string is parsed as midnight UTC. In any time zone west of UTC, midnight UTC is still the previous day, so the birthday shows a day early. For calendar dates, don’t turn them into a Date at all – keep the string, or split it:
const [y, m, d] = "2026-11-20".split("-").map(Number)
const local = new Date(y, m - 1, d) // midnight in the user's time zone
local.toLocaleDateString("en-CA") // "2026-11-20"Trap 2: a missing time zone means local time
new Date("2026-11-20T10:00").toISOString() // "2026-11-20T15:00:00.000Z" (local, New York)
new Date("2026-11-20T10:00:00Z").toISOString() // "2026-11-20T10:00:00.000Z" (UTC)
new Date("2026-11-20T10:00:00+02:00").toISOString() // "2026-11-20T08:00:00.000Z"The same text gives a different moment on every user’s computer when the zone is missing. Always include Z or an offset in timestamps you send.
Trap 3: JSON.stringify converts to UTC
JSON.stringify(new Date(2026, 10, 20)) // '"2026-11-20T05:00:00.000Z"'Local midnight on 20 November in New York is 05:00 UTC, and that’s what gets sent. That is correct for a moment in time, but a server that only keeps the date part may now store the wrong day in other time zones (for example in Asia, where local midnight is the previous day in UTC). Send calendar dates as "YYYY-MM-DD" strings instead of Date objects.
Trap 4: seconds vs milliseconds
new Date(1795165200).toISOString() // "1970-01-21T18:39:25.200Z" ✗
new Date(1795165200 * 1000).toISOString() // "2026-11-20T09:00:00.000Z" ✓Unix timestamps are usually in seconds (10 digits today); JavaScript’s Date uses milliseconds (13 digits). A date in January 1970 almost always means a timestamp in seconds was read as milliseconds. If your API uses numbers, say which unit in the field name: "createdAtMs" or "created_at_unix".
exp and iat claims are Unix timestamps in seconds. The JWT Decoder shows them as readable dates.Python
import datetime as dt
# naive datetime: no zone – ambiguous, avoid for timestamps
dt.datetime(2026, 11, 20, 10, 0).isoformat()
# '2026-11-20T10:00:00'
# aware datetime in UTC
dt.datetime(2026, 11, 20, 10, 0, tzinfo=dt.timezone.utc).isoformat()
# '2026-11-20T10:00:00+00:00'
# the same format JavaScript produces
dt.datetime(2026, 11, 20, 10, 0, tzinfo=dt.timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z")
# '2026-11-20T10:00:00.000Z'
# Unix timestamps
dt.datetime.fromtimestamp(1795165200, tz=dt.timezone.utc).isoformat()
# '2026-11-20T09:00:00+00:00'Parsing the Z suffix depends on your Python version:
dt.datetime.fromisoformat("2026-11-20T10:00:00Z")
# Python 3.11+: datetime(2026, 11, 20, 10, 0, tzinfo=timezone.utc)
# Python 3.10 and older: ValueError: Invalid isoformat string: '2026-11-20T10:00:00Z'
# works everywhere:
dt.datetime.fromisoformat("2026-11-20T10:00:00Z".replace("Z", "+00:00"))json.dumps can’t serialize datetimes at all (“Object of type datetime is not JSON serializable”). See the Python JSON guide for a default function that writes ISO strings.
Checklist for your API
- Timestamps: ISO 8601 with
Zor an offset. Store them in UTC and convert to local time only when displaying. - Calendar dates:
"YYYY-MM-DD", kept as strings in the client. - Numbers: only if documented, with the unit in the name.
- Never use local formats like 20/11/2026.
- Converting back: use a reviver in
JSON.parse(see JSON.stringify: hidden rules) orobject_hookin Python.