How to represent date and/or time information in JSON?
Asked Answered
A

5

19

JSON text (RFC 4627) has unambigious representation of objects, arrays, strings, numbers, Boolean values (literally true or false) and null. However, it has nothing defined for representing time information like date and time of day, which is very common in applications. What are the current methods in use to represent time in JSON given the constraints and grammar laid out in RFC 4627?

Note to respondents: The purpose of this question is to document the various methods known to be in circulation along with examples and relative pros and cons (ideally from field experience).

Algetic answered 18/9, 2008 at 10:13 Comment(0)
R
13

The only representation that I have seen in use (though, admittedly, my experience is limited to DOJO) is ISO 8601, which works nicely, and represents just about anything you could possibly think of.

For examples, you can visit the link above.

Pros:

  • Represents pretty much anything you could possibly throw at it, including timespans. (ie. 3 days, 2 hour)

Cons:

  • Umm... I don't know actually. Other than perhaps it might take a bit of getting used to? It's certainly easy enough to parse, if there aren't built in functions to parse it already.
Rattlepate answered 18/9, 2008 at 10:29 Comment(0)
N
5

ISO 8601 seems like a natural choice, but if you'd like to parse it with JavaScript running in a browser, you will need to use a library, for browser supports for the parts of the JavaScript Date object that can parse ISO 8601 dates is inconsistent, even in relatively new browsers. Another problem with ISO 8601 is that it is a large, rich standard, and the date/time libraries support only part of it, so you will have to pick a subset of ISO 8601 to use that is supported by the libraries you use.

Instead, I represent times as the number of milliseconds since 1970-01-01T00:00Z. This is understood by the constructor for the Date object in much older browsers, at least going back to IE7 (which is the oldest I have tested).

Nowak answered 5/6, 2012 at 15:19 Comment(0)
R
2

There is no set literal so use what's easiest for you. For most people, that's either a string of the UTC output or an long-integer of the UTC-centered timecode.

Read this for a bit more background: http://msdn.microsoft.com/en-us/library/bb299886.aspx

Regress answered 18/9, 2008 at 10:19 Comment(0)
D
0

I recommend using RFC 3339 format, which is nice and simple, and understood by an increasing number of languages, libraries, and tools.

Unfortunately, RFC 3339, Unix epoch time, and JavaScript millisecond time, are all still not quite accurate, since none of them account for leap seconds! At some point we're all going to have to revisit time representations yet again. Maybe the next time we can be done with it.

Dejected answered 23/12, 2014 at 23:46 Comment(0)
O
0

Sorry to comment on such an old question, but in the intervening years more solutions have turned up.

Representing date and/or time information in JSON is a special case of the more general problem of representing complex types and complex data structures in JSON. Part of what make the problem tricky is that if you represent complex types like timestamps as JSON objects, then you need to have a way of expression associative arrays and objects, which happen to look like your JSON object representation of a timestamp, as some other marked-up object.

Google's protocol buffers have a JSON mapping which has the notion of a timestamp type, with defined semantics.

MongoDB's BSON has an Extended JSON which says { "$date": "2017-05-17T23:09:14.000000Z" }.

Both can also express way more complex structures in addition to datetime.

Ortrud answered 18/5, 2017 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.