snippetjavascriptCritical
What is the "right" JSON date format?
Viewed 0 times
daterighttheformatwhatjson
Problem
I've seen so many different standards for the JSON date format:
Which one is the right one? Or best? Is there any sort of standard on this?
"\"\\/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 8601Which one is the right one? Or best? Is there any sort of standard on this?
Solution
JSON itself does not specify how dates should be represented, but JavaScript does.
You should use the format emitted by
Here's why:
-
It's human readable but also succinct
-
It sorts correctly
-
It includes fractional seconds, which can help re-establish chronology
-
It conforms to ISO 8601
-
ISO 8601 has been well-established internationally for more than a decade
-
ISO 8601 is endorsed by W3C, RFC3339, and XKCD
That being said, every date library ever written can understand "milliseconds since 1970". So for easy portability, ThiefMaster is right.
You should use the format emitted by
Date's toJSON method:2012-04-23T18:25:43.511ZHere's why:
-
It's human readable but also succinct
-
It sorts correctly
-
It includes fractional seconds, which can help re-establish chronology
-
It conforms to ISO 8601
-
ISO 8601 has been well-established internationally for more than a decade
-
ISO 8601 is endorsed by W3C, RFC3339, and XKCD
That being said, every date library ever written can understand "milliseconds since 1970". So for easy portability, ThiefMaster is right.
Context
Stack Overflow Q#10286204, score: 2533
Revisions (0)
No revisions yet.