Coder Perfect

What JSON date format is “proper”?

Problem

For the JSON date format, I’ve seen a lot of various standards:

"\"\\/Date(1335205592410)\\/\""         .NET JavaScriptSerializer
"\"\\/Date(1335205592410-0500)\\/\""    .NET DataContractJsonSerializer
"2012-04-23T18:25:43.511Z"              JavaScript built-in JSON object
"2012-04-21T18:25:43-05:00"             ISO 8601

Which option is the correct one? Or is it better? Is there a set of rules for this?

Asked by Kamyar Nazeri

Solution #1

Although JSON does not dictate how dates should be displayed, JavaScript does.

The format returned by Date’s toJSON function should be used:

04/23/2012 18:25:43

Here’s why:

That said, “milliseconds since 1970” is understood by every date library ever developed. ThiefMaster is the best choice for portability.

Answered by funroll

Solution #2

JSON isn’t aware of the concept of dates. What.NET does is a hack/extension that isn’t standard.

I’d go with a format that’s easy to convert to a Date object in JavaScript, i.e. one that can be handed to new Date objects (…). The timestamp including milliseconds since 1970 is the simplest and most portable format.

Answered by ThiefMaster

Solution #3

There is no one-size-fits-all format for sharing dates; the JSON specification does not provide one, which is why there are so many distinct approaches.

The optimum format is probably a date in ISO 8601 format (see Wikipedia); it’s a well-known and extensively used format that can be handled in a variety of languages, making it ideal for interoperability. Choosing 8601 as the date interchange format is a suitable choice if you have control over the generated json, for example, if you provide data to other systems in json format.

If you don’t have control over the generated json, such as if you’re a consumer of json from multiple distinct systems, having a date parsing utility function to handle the various forms required is the best way to go.

Answered by Russ Cam

Solution #4

The I-JSON Message Format (RFC 7493) states:

Depending on who you ask, I-JSON stands for either Internet JSON or Interoperable JSON.

Answered by Bryan Larsen

Solution #5

When in doubt, simply hit F12 (Ctrl+Shift+K in Firefox) to open the javascript web console of a contemporary browser and type the following:

new Date().toISOString()

Will output:

Ta-da!!

Answered by Shayan Ahmad

Post is based on https://stackoverflow.com/questions/10286204/what-is-the-right-json-date-format