JS Built-ins
← Date111 / 120
DatetoJSONbeginner

toJSON() β€” epoch returns ISO string

πŸ“– 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()

Returns the same ISO 8601 string as toISOString(). Useful for JSON serialization.

Challenge: Verify that new Date(0).toJSON() returns '1970-01-01T00:00:00.000Z'.

πŸ’‘ Hints (1)
  • β€’ toJSON() calls toISOString() internally β€” they produce the same output for valid dates