JS Built-ins
← Date114 / 120
DatetoJSONbeginner

toJSON() β€” ends with "Z"

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

Like toISOString(), the result always ends with "Z" to indicate UTC.

Challenge: Verify that new Date(0).toJSON().endsWith('Z') is true.

πŸ’‘ Hints (1)
  • β€’ "Z" means UTC/Zulu time β€” toJSON() always serializes in UTC