Any suggestions for how to handle Dates in JSON?

Jan 31, 2014 22:08

Because JSON doesn't support a standard date format I'm going to have to serialise/deserialise to _something_. A quick google has found me numerous different approaches - anyone care to point me at a specific best practice ( Read more... )

Leave a comment

Comments 13

andrewducker January 31 2014, 22:11:51 UTC
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx seems to indicate that I should be using ISO 8601. Which seems like a plausible route to take.

Reply

ciphergoth January 31 2014, 22:17:52 UTC
There are *lots* of ways of writing dates in ISO 8601. I'd use specifically UTC RFC 3339 dates: 1985-04-12T23:20:50.52Z

Reply


skington February 1 2014, 01:28:01 UTC
I don't know what libraries / languages you're using, or what's easiest, but thankfully as you're talking about JSON that doesn't matter. JSON is a serialisation format, so all that matters is that your data is obvious, unsurprising and easy.

So I'd say a choice between SQL DATETIME format (yyyy-mm-dd hh:mm:ss) and, as mentioned elsewhere, ISO 8601. I mildly prefer DATETIME because it's more legible than ISO 8601, but meh.

Above all, (1) don't use stupid US-style date formats, and (2) store all dates, ever, in UTC. Do not fuck about with time zones.

Reply

momentsmusicaux February 1 2014, 08:06:30 UTC
> Above all, (1) don't use stupid US-style date formats, and (2) store all dates, ever, in UTC. Do not fuck about with time zones.

Yes to both, absolutely.

If you need to output in a specific timezone, store that fact as a separate piece of data, and convert on output.

Reply

channelpenguin February 1 2014, 13:52:47 UTC
YES YES YES to these 2

Reply


apostle_of_eris February 1 2014, 05:06:00 UTC
+1

Reply


mark_nicol February 1 2014, 06:22:49 UTC
Another +1 - No-one ever got cursed for keeping things simple and legible

Reply


momentsmusicaux February 1 2014, 08:05:19 UTC
Unix timestamp? Or do you need partial dates?

Otherwise, the ISO format, which does. (2014-02-01T08:04:00)

If your language has serialization of its own, then you could use that and just give JSON the string (as you could in PHP, if you had DateTime objects).

Reply

andrewducker February 1 2014, 15:16:18 UTC
Yeah, I think Unix Timestamp is the way to go.

My two languages are Java (which can easily convert to/from Epoch) and Javascript (ditto).

Reply

momentsmusicaux February 1 2014, 15:25:29 UTC
The downside with unix timestamp is that it's a PITA for humans to read.

I have lost count of the number of times I've had to type FROM_UNIX() or whatever the SQL function is to make my database queries produce something I can actually understand when debugging.

Reply

andrewducker February 1 2014, 15:32:41 UTC
True.

But on the other hand, I'm converting it into a JS Date at the point where it's being read into the UI. So I'm never actually having to deal with a timestamp by eye - I'm either seeing it in the Java debugger as a DateTime object, or in the JS debugger as a Date object.

If I was writing it for something other people would use from different front ends then yeah, readability would definitely be a factor.

Reply


Leave a comment

Up