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

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

andrewducker February 1 2014, 15:36:48 UTC
Ok, you talked me into it. It was a two line change to move it over to the readable version, and the JS side still works perfectly. So I've done that :->

Reply


Leave a comment

Up