Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Best Practice for Encoding Dates in JSON?

When designing a service that returns JSON to the browser (or server side JS), what is the "best" format for encoding dates in the resulting JSON response?

The data property should:

  • require as little or no parsing to convert to a Date() object
  • be sortable in the JSON representation itself

Returning the number of milliseconds since 1 January 1970 00:00:00 UTC seems like it would be the best option but is not particularly human readable when looking at the raw JSON.

What's preferred in your experience and why?

like image 691
mbrevoort Avatar asked Jan 13 '11 15:01

mbrevoort


1 Answers

Personally I'd go with ISO 8601 (e.g. 2011-01-13T14:09Z).

It's well supported by virtually every programming language around, it's fairly easily human readable and, if you absolutely have to roll your own parser/formatter, it's fairly easy to deal with there too. It also has the sortable property you wanted.

like image 50
Brian Donovan Avatar answered Nov 14 '22 22:11

Brian Donovan