简体   繁体   中英

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?

Personally I'd go with ISO 8601 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM