jsondecode.com logo

Fix: JSON Date Format

JSON has no native date type. Dates are serialized as strings or numbers, and JSON.parse() does not automatically convert them back to Date objects. This causes subtle bugs where code expects a Date but gets a string.

How JSON.stringify() Handles Dates

JSON.stringify() automatically calls .toISOString() on any Date object it encounters:

const event = {
  name: "Launch",
  date: new Date("2024-03-15T10:30:00"),
};

JSON.stringify(event);
// => '{"name":"Launch","date":"2024-03-15T10:30:00.000Z"}'
//                                                    ↑ UTC, always Z suffix

Note: .toISOString() always returns UTC. If you create new Date("2024-03-15T10:30:00") in a local timezone, the serialized string will be converted to UTC.

Why JSON.parse() Returns Strings, Not Dates

const json = '{"name":"Launch","date":"2024-03-15T10:30:00.000Z"}';

const parsed = JSON.parse(json);
console.log(parsed.date);           // "2024-03-15T10:30:00.000Z" ← string
console.log(typeof parsed.date);    // "string", NOT "object"

// This will throw because strings don't have .getFullYear()
parsed.date.getFullYear();
// TypeError: parsed.date.getFullYear is not a function

Reviver Function — Auto-Revive Date Strings

Pass a reviver as the second argument to JSON.parse() to convert ISO strings back to Date objects:

const ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;

function dateReviver(key: string, val: unknown): unknown {
  if (typeof val === "string" && ISO_DATE_RE.test(val)) {
    return new Date(val);
  }
  return val;
}

const json = '{"name":"Launch","date":"2024-03-15T10:30:00.000Z"}';
const parsed = JSON.parse(json, dateReviver);

console.log(parsed.date instanceof Date); // true
console.log(parsed.date.getFullYear());   // 2024

Python: Serializing datetime Objects

Python's json.dumps() raises TypeError for datetime objects unless you provide a default handler:

import json
from datetime import datetime

event = {"name": "Launch", "date": datetime(2024, 3, 15, 10, 30)}

# Simple approach: convert everything to string
print(json.dumps(event, default=str))
# => {"name": "Launch", "date": "2024-03-15 10:30:00"}

# ISO 8601 format
print(json.dumps(event, default=lambda x: x.isoformat() if hasattr(x, "isoformat") else str(x)))
# => {"name": "Launch", "date": "2024-03-15T10:30:00"}

# Parse back
raw = json.loads('{"date": "2024-03-15T10:30:00"}')
dt = datetime.fromisoformat(raw["date"])
print(dt.year)  # 2024

ISO 8601 Format Reference

FormatExampleNotes
Date only2024-03-15No time, no timezone
Date + time (UTC)2024-03-15T10:30:00ZZ = UTC
Date + time + ms (UTC)2024-03-15T10:30:00.000ZtoISOString() default output
Date + time + offset2024-03-15T10:30:00+05:30Explicit timezone offset
Unix timestamp (ms)1710498600000Number, always UTC, compact
Unix timestamp (s)1710498600Number, divide by 1000 in JS

Timezone Gotchas

// Beware: date-only strings are parsed as UTC midnight
new Date("2024-03-15").toISOString();
// => "2024-03-15T00:00:00.000Z"  ← UTC

// But date+time without Z is parsed as LOCAL time
new Date("2024-03-15T10:30:00").toISOString();
// => "2024-03-15T05:00:00.000Z"  ← shifted if you are in UTC+5:30

// Always append Z or an offset for unambiguous UTC:
new Date("2024-03-15T10:30:00Z").toISOString();
// => "2024-03-15T10:30:00.000Z"  ← correct UTC

Inspect Your JSON Below

Paste JSON containing date strings to validate the format.

Input JSON
Formatted Output
Formatted JSON will appear here

Frequently Asked Questions

Does JSON have a Date type?

No. The JSON specification (RFC 8259) defines only six types: string, number, boolean, null, object, and array. There is no native date type. Dates are conventionally stored as ISO 8601 strings (e.g. "2024-03-15T10:30:00.000Z") or as Unix timestamps (numbers representing seconds or milliseconds since epoch).

What does JSON.stringify() do with a JavaScript Date object?

JSON.stringify() calls .toISOString() on Date objects, which outputs a UTC string in the format "YYYY-MM-DDTHH:mm:ss.sssZ". For example, new Date("2024-03-15") becomes "2024-03-15T00:00:00.000Z". The Z suffix indicates UTC. This happens automatically — you do not need a replacer for serialization.

Why does JSON.parse() not convert date strings back to Date objects?

JSON.parse() has no way to distinguish an ISO date string like "2024-03-15T10:30:00.000Z" from any other string. To revive dates automatically, pass a reviver function as the second argument: JSON.parse(json, (key, val) => typeof val === "string" && /^\d{4}-\d{2}-\d{2}T/.test(val) ? new Date(val) : val).

Should I store dates as ISO strings or Unix timestamps in JSON?

Both approaches are common. ISO 8601 strings ("2024-03-15T10:30:00.000Z") are human-readable and timezone-explicit. Unix timestamps (numbers like 1710498600000) are compact and easy to compare/sort arithmetically. Use ISO strings when human readability matters (logs, APIs); use integers for compact storage or when doing date arithmetic in JavaScript.

How do I handle JSON dates in Python?

Python's json.dumps() does not serialize datetime objects by default — it raises TypeError. Pass a default function: json.dumps(obj, default=str) converts datetime to its string representation. To parse, use json.loads() and then datetime.fromisoformat() on the string fields you expect to be dates. The python-dateutil library handles more ISO 8601 edge cases.

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.