Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 430
Atif Aziz Avatar asked Sep 18 '08 10:09

Atif Aziz


People also ask

How is time represented in JSON?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format.

Does JSON have DateTime?

Json library parses and writes DateTime and DateTimeOffset values according to the ISO 8601-1:2019 extended profile.


1 Answers

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.
like image 86
Matthew Scharley Avatar answered Oct 27 '22 11:10

Matthew Scharley