The “right” JSON Date Format

JSON is prominent for its prevalent serialization format. For Web-based applications, it’s become the serialization format. The problem with dates in JSON is that it does not specify date representation.

To represent dates in JavaScript, JSON uses ISO 8601 string format to encode dates as a string. Dates are encoded as ISO 8601 strings and then treated just like a regular string when the JSON is serialized and deserialized. Choosing the ISO 8601 as a data interchange is an excellent experience if you provide data to other systems in JSON format. There is serialization to this format, but no direct deserialization to date from it.

You should use the format emitted by JavaScript Date's toJSON method:

"2020-03-09T22:18:26.625Z"

The date is represented in a standard and sortable format that represents a UTC time.

This is also the preferred representation according to ECMA using the JSON.stringify:

let jsonStr = JSON.stringify({'now': new Date()}) ;   
console.log(jsonStr);

ISO 8601 Format

ISO 8601 Format (International Organization for Standardization) is an international standard that covers the exchange of date and time-related data. This standard provides a well-defined method of representing dates and times, so as to avoid misinterpretation of numeric representations of dates and times.

Do you find this helpful?

Related articles