JavaScript Date toJSON() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Date

Definition

Date.prototype.toJSON() returns a string for this instant in the same UTC ISO format as toISOString() (for example "1975-08-19T23:15:30.000Z"). JSON.stringify() calls it automatically when a Date appears in the value being serialized.

Remember
date.toJSON()

→ "1975-08-19T23:15:30.000Z"   (valid Date — same as toISOString)
→ null                         (invalid Date)

JSON.stringify(date)
→ "\"1975-08-19T23:15:30.000Z\""   // calls toJSON() for you

toISOString()  → throws on invalid
toJSON()       → null on invalid

Syntax

Call date.toJSON. Start with this line.

JavaScript
date.toJSON()
CallWhat it does
date.toJSON()Primary signature.
date.toJSON(...)Call with the arguments your case needs.

Built into JavaScript. Call toJSON(...) on a Date instance — no import required.

Library declarations
JavaScript
date.toJSON()

Parameters

ParameterDescription
JSON.stringifyOptional. Calls toJSON() automatically

Return Value

Return ValueDescription
*The value returned by date.toJSON().

JavaScript Examples

Example 1 — Basic toJSON() Output

Call toJSON() on a UTC instant — same shape as toISOString().

JavaScript
const event = new Date("August 19, 1975 23:15:30 UTC");

console.log(event.toJSON());
// "1975-08-19T23:15:30.000Z"

console.log(event.toISOString());
// "1975-08-19T23:15:30.000Z"
Try It Yourself

How It Works

  1. Build a Date. The string includes UTC, so the instant is unambiguous.
  2. Call toJSON. Returns the portable UTC ISO string ending in Z.
  3. Compare toISOString. For valid dates, both methods return the same text.
  4. No side effects. The Date is unchanged; only a string is returned.

Example 2 — JSON.stringify Calls toJSON()

Put a Date inside an object and let stringify serialize it.

JavaScript
const payload = {
  title: "Lesson",
  createdAt: new Date(Date.UTC(2026, 2, 15, 12, 0, 0))
};

console.log(JSON.stringify(payload));
// {"title":"Lesson","createdAt":"2026-03-15T12:00:00.000Z"}
Try It Yourself

How It Works

  1. Store a Date field. createdAt holds a real Date object.
  2. Stringify the object. JSON.stringify finds toJSON on that value.
  3. Replace with ISO text. The JSON field becomes a quoted UTC string.
  4. After parse. JSON.parse gives you a string again — rebuild with new Date(...) if needed.

Example 3 — Invalid Date Returns null

See how toJSON() differs from toISOString() on bad input.

JavaScript
const bad = new Date("not-a-date");

console.log(bad.toJSON()); // null

try {
  console.log(bad.toISOString());
} catch (err) {
  console.log(err.name); // RangeError
}

console.log(JSON.stringify({ t: bad }));
// {"t":null}
Try It Yourself

How It Works

  1. Create an invalid Date. The time value is NaN.
  2. Call toJSON. Non-finite time → returns null (no throw).
  3. Call toISOString. Same bad date → RangeError.
  4. Stringify quietly. {"t":null} can hide bad data — validate before sending APIs.

Browser & Runtime Support

Date.prototype.toJSON() has been in JavaScript since ES5. It is Baseline and works in every modern browser and Node.js — no polyfill needed for current targets.

✓ Baseline · ES5

Date.prototype.toJSON()

Supported in Chrome, Firefox, Safari, Edge, Internet Explorer 9+, and all modern Node.js versions.

99% Universal API
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Date.toJSON() Excellent

Bottom line: Safe everywhere JSON.stringify exists. Remember: invalid dates become null, not an error. See also the MDN toJSON() reference.

Frequently Asked Questions

For a valid Date, the same UTC ISO 8601 string as toISOString() (for example "1975-08-19T23:15:30.000Z"). For an invalid Date, it returns null.
JSON.stringify() calls toJSON() on any value that defines it. That is why Date objects inside objects and arrays become ISO strings in JSON output.
For valid dates, yes — toJSON() returns this.toISOString(). The big difference is invalid dates: toJSON() returns null; toISOString() throws RangeError.
After JSON.parse, the value is a plain string. Pass it to new Date(isoString), or use a JSON.parse reviver that rebuilds Date fields you expect.
No. It is read-only — it returns a string (or null) and leaves the Date unchanged.
Because toJSON() returns null for non-finite time values, and JSON.stringify uses that return value. Prefer validating dates before you serialize.

Next: toLocaleDateString()

Learn how to format a local calendar date with Intl-friendly locale rules.

toLocaleDateString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful