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... )
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.
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).
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.
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.
Comments 13
Reply
Reply
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
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
Reply
Reply
Reply
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
My two languages are Java (which can easily convert to/from Epoch) and Javascript (ditto).
Reply
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
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