What is the "right" JSON date format?
JSON itself does not specify how dates should be represented, but JavaScript does.
You should use the format emitted by Date
's toJSON
method:
2012-04-23T18:25:43.511Z
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.
JSON does not know anything about dates. What .NET does is a non-standard hack/extension.
I would use a format that can be easily converted to a Date
object in JavaScript, i.e. one that can be passed to new Date(...)
. The easiest and probably most portable format is the timestamp containing milliseconds since 1970.
There is no right format; The JSON specification does not specify a format for exchanging dates which is why there are so many different ways to do it.
The best format is arguably a date represented in ISO 8601 format (see Wikipedia); it is a well known and widely used format and can be handled across many different languages, making it very well suited for interoperability. If you have control over the generated json, for example, you provide data to other systems in json format, choosing 8601 as the date interchange format is a good choice.
If you do not have control over the generated json, for example, you are the consumer of json from several different existing systems, the best way of handling this is to have a date parsing utility function to handle the different formats expected.