JS Built-ins
← Date112 / 120
DatetoJSONintermediate

toJSON() β€” same as toISOString()

πŸ“– Usage Example
const d = new Date('2024-01-15T00:00:00.000Z')
d.toJSON()  // β†’ '2024-01-15T00:00:00.000Z'
JSON.stringify({ date: d })  // uses toJSON()

Use toJSON() to get the ISO 8601 UTC string representation of a Date; it is called automatically by JSON.stringify.

Date.prototype.toJSON()

toJSON() and toISOString() return the same string for a valid Date.

Challenge: Verify that new Date(0).toJSON() === new Date(0).toISOString().

πŸ’‘ Hints (1)
  • β€’ Under the hood, toJSON() delegates to toISOString()