Date.prototype.toJSON()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThetoJSON() method ofDate instances returns a string representing this date in the same ISO format astoISOString().
In this article
Try it
const event = new Date("August 19, 1975 23:15:30 UTC");const jsonDate = event.toJSON();console.log(jsonDate);// Expected output: "1975-08-19T23:15:30.000Z"console.log(new Date(jsonDate).toUTCString());// Expected output: "Tue, 19 Aug 1975 23:15:30 GMT"Syntax
toJSON()Parameters
None.
Return value
A string representing the given date in thedate time string format according to universal time, ornull when the date isinvalid. For valid dates, the return value is the same as that oftoISOString().
Description
ThetoJSON() method is automatically called byJSON.stringify() when aDate object is stringified. This method is generally intended to, by default, usefully serializeDate objects duringJSON serialization, which can then be deserialized using theDate() constructor as the reviver ofJSON.parse().
The method first attempts to convert itsthis valueto a primitive by calling its[Symbol.toPrimitive]() (with"number" as hint),valueOf(), andtoString() methods, in that order. If the result is anon-finite number,null is returned. (This generally corresponds to an invalid date, whosevalueOf() returnsNaN.) Otherwise, if the converted primitive is not a number or is a finite number, the return value ofthis.toISOString() is returned.
Note that the method does not check whether thethis value is a validDate object. However, callingDate.prototype.toJSON() on non-Date objects fails unless the object's number primitive representation isNaN, or the object also has atoISOString() method.
Examples
>Using toJSON()
const jsonDate = new Date(0).toJSON(); // '1970-01-01T00:00:00.000Z'const backToDate = new Date(jsonDate);console.log(jsonDate); // 1970-01-01T00:00:00.000ZSerialization round-tripping
When parsing JSON containing date strings, you can use theDate() constructor to revive them into the original date objects.
const fileData = { author: "Maria", title: "Date.prototype.toJSON()", createdAt: new Date(2019, 3, 15), updatedAt: new Date(2020, 6, 26),};const response = JSON.stringify(fileData);// Imagine transmission through networkconst data = JSON.parse(response, (key, value) => { if (key === "createdAt" || key === "updatedAt") { return new Date(value); } return value;});console.log(data);Note:The reviver ofJSON.parse() must be specific to the payload shape you expect, because the serialization isirreversible: it's not possible to distinguish between a string that represents a Date and a normal string.
Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-date.prototype.tojson> |