Published Dec 22, 2023
Contribute to Docs

The method .toJSON() takes a Date object and returns a string representation of that Date in the JavaScript date time string format. This format is as follows:


Required elements:

  • YYYY: Four digits representing a year.

Optional elements:

  • MM: Two digits representing a month.
  • DD: Two digits representing a day.
  • HH: Two digits representing the number of hours since midnight of the day above.
  • mm: Two digits representing the number of minutes since the hour above.
  • ss: Two digits representing the number of seconds since the minute above.
  • sss: Three digits representing the number of milliseconds since the second above.
  • Z: A character or series of characters representing a time zone offset from UTC.

.toJSON() gives an equivalent return value to .toISOString() as long the value of the Date object is valid. If the value is invalid, .toJSON() will return null, whereas .toISOString() will throw a RangeError.


  • date: The Date object which .toJSON() returns a string representation of.


The following example declares a Date object and passes it to .toJSON().

const meleeDay = new Date('2001-11-21');

The above example will give the following output:


Codebyte Example

Try changing the value of yourDate below to test the behavior of .toJSON():

Visit us
Hide code
Hide output
Hide output

All contributors

Looking to contribute?

Learn JavaScript on Codecademy