TExcelTAD
Home › JSON › JSON and Dates

JSON and Dates

JSON has no date type. Yet almost every application needs to exchange dates and times — order dates, birthdays, log timestamps. The solution everyone agrees on is simple: write dates as strings in ISO 8601 format, and convert them back to date objects after parsing.

ISO 8601 strings

ISO 8601 is an international standard for writing dates and times:

Format Example Meaning
Date only "2025-06-01" 1 June 2025
Date and time in UTC "2025-06-01T14:30:00Z" Z = UTC ("Zulu")
With milliseconds "2025-06-01T14:30:00.250Z"
With an offset "2025-06-01T21:30:00+07:00" same instant, Vietnam time

These strings have two big advantages: they are unambiguous worldwide (no "is 03/04 March or April?"), and they sort correctly as plain text.

json · try it yourself
{
  "event": "Product launch",
  "date": "2025-06-01",
  "startsAt": "2025-06-01T14:30:00Z",
  "localStart": "2025-06-01T21:30:00+07:00"
}
Valid JSON: object with 4 keys
{
  "event": "Product launch",
  "date": "2025-06-01",
  "startsAt": "2025-06-01T14:30:00Z",
  "localStart": "2025-06-01T21:30:00+07:00"
}

To JSON, these are just strings — the validator has no idea they are dates. Your code has to agree on the meaning.

Date → JSON: automatic

A JavaScript Date has a toJSON() method, so JSON.stringify turns it into an ISO string in UTC automatically:

js · try it yourself
const meeting = {
  title: "Sprint review",
  at: new Date(Date.UTC(2025, 2, 14, 9, 0)),  // months are 0-based: 2 = March
};

const text = JSON.stringify(meeting);
console.log(text);
console.log(meeting.at.toJSON() === meeting.at.toISOString());
{"title":"Sprint review","at":"2025-03-14T09:00:00.000Z"}
true

JSON → Date: not automatic

Parsing gives you the string back, not a Date:

js · try it yourself
const meeting = JSON.parse('{"title": "Sprint review", "at": "2025-03-14T09:00:00.000Z"}');

console.log(typeof meeting.at);
// meeting.at.getUTCFullYear()  -> TypeError: not a function

const at = new Date(meeting.at);
console.log(at.getUTCFullYear(), at.getUTCMonth() + 1, at.getUTCDate());
string
2025 3 14

Reviving dates automatically

Converting every field by hand gets tedious. A reviver can recognise ISO strings and convert them while parsing:

js · try it yourself
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;

function dateReviver(key, value) {
  return typeof value === "string" && ISO.test(value) ? new Date(value) : value;
}

const text = `{
  "orderId": 88,
  "createdAt": "2025-01-05T08:00:00Z",
  "shipments": [
    {"carrier": "VNPost", "shippedAt": "2025-01-06T10:15:00+07:00"}
  ],
  "note": "2025 is not a date"
}`;

const order = JSON.parse(text, dateReviver);
console.log(order.createdAt instanceof Date);
console.log(order.shipments[0].shippedAt.toISOString());
console.log(typeof order.note);

const days = (order.shipments[0].shippedAt - order.createdAt) / 86_400_000;
console.log("Days to ship:", days.toFixed(2));
true
2025-01-06T03:15:00.000Z
string
Days to ship: 0.80

Tip: Only revive keys you know are dates (for example keys ending in At), or a user-typed string that happens to look like a date would be converted too.

Other ways to send dates

Approach Example Pros / cons
ISO 8601 string "2025-03-14T09:00:00Z" Readable, sortable, standard — recommended
Unix timestamp (seconds) 1741942800 Compact, but unreadable and ambiguous (seconds or ms?)
Milliseconds since 1970 1741942800000 What Date.getTime() returns
Date-only string "2025-03-14" Best for birthdays — no time zone surprises
js · try it yourself
const ms = Date.UTC(2025, 2, 14, 9, 0);
console.log(ms);                        // milliseconds
console.log(ms / 1000);                 // Unix seconds
console.log(new Date(ms).toISOString());
1741942800000
1741942800
2025-03-14T09:00:00.000Z

Time zone pitfalls

  • Store and send UTC (Z) or include an explicit offset. A string like "2025-03-14T09:00:00" without a zone is interpreted as local time by JavaScript — so it means a different instant on every computer.
  • A date-only string like "2025-03-14" is parsed as UTC midnight. Displayed in a time zone west of UTC, it shows up as the previous day.
  • Convert to local time only when displaying dates to users.

Dates in Python

Python's json module refuses datetime objects unless you tell it how to convert them:

python · try it yourself
import json
from datetime import datetime, timezone

event = {"name": "Deploy", "at": datetime(2025, 3, 14, 9, 0, tzinfo=timezone.utc)}

text = json.dumps(event, default=lambda o: o.isoformat())
print(text)

back = json.loads(text)
at = datetime.fromisoformat(back["at"])
print(at.year, at.month, at.tzinfo)
{"name": "Deploy", "at": "2025-03-14T09:00:00+00:00"}
2025 3 UTC

Test yourself

What is the recommended way to store a date-time in JSON?

  1. A Date object
  2. An ISO 8601 string like "2025-03-14T09:00:00Z"
  3. "14/03/2025 9am"
  4. An array [2025, 3, 14]

What type is JSON.parse('{"d":"2025-01-01T00:00:00Z"}').d?

  1. Date
  2. number
  3. string
  4. object

What does the Z in 2025-06-01T14:30:00Z mean?

  1. Zero milliseconds
  2. UTC time
  3. Local time
  4. The end of the string