Date objects in JSON

hingo's picture

Flavio of MongoDB fame complained in a tweet about wanting a date format in JSON. Currently you can only express strings and numbers as the primitive data types in JSON, as well the composite forms of a list: [] and an object: {}. The latter is essentially just a subhierarchy in the object/array that is your full JSON document.

i wonder why there's no "STANDARD" way to (de)serialize times / date times into / from json! They might not be "primitive" but COME ON!!

— FlaPer87 (@flaper87) July 20, 2012

My response:

@flaper87 Going back to basics: is there a json-like string that you can eval() and get a JS Date object?

— Henrik Ingo (@h_ingo) July 20, 2012

What did I mean by that?

Json serialization was originally invented in JavaScript circles. The point of the Json format is that it is valid JavaScript syntax. Originally, de-serialization was done with the eval() function:


<script type="text/javascript">
var jsondoc = '{"hello" : "world", "foo" : "bar", "nr" : 1 }'
var obj;
eval("obj = " + jsondoc);
</script>

So my point is, whatever date-format you want to standardize for Json, it needs to at least be valid JavaScript syntax, so that a user could eval() the string and actually get a Date() object. Something like a simple:


var d = new Date("2012-07-22T19:49:00");

In fact, that is perfectly possible (tested on Firefox) to put into a json-like string:


<!DOCTYPE html>
<html>
<body>

<h1>Demo

<p id="one">A Paragraph.

<p id="two">A Paragraph.

<p id="three">A Paragraph.

<p id="four">A Paragraph.

<p id="five">A Paragraph.

<p id="six">A Paragraph.

<p id="seven">A Paragraph.

<script type="text/javascript">

var d;
d = new Date("2012-07-22T19:49:00");

var jsondoc = '{"hello" : "world", "foo" : "bar", "nr" : 1, "timestamp" : "2012-07-22T19:49:00", "dateobj_ref" : d, "dateobj_inline" : new Date("2012-07-22T19:49:00") }'
var obj;
eval("obj = " + jsondoc);

document.getElementById("one").innerHTML=obj.hello;
document.getElementById("two").innerHTML=obj.foo;
document.getElementById("three").innerHTML=obj.nr;
document.getElementById("four").innerHTML=obj.timestamp;
document.getElementById("five").innerHTML=obj.dateobj_ref;
document.getElementById("six").innerHTML=obj.dateobj_inline;
document.getElementById("seven").innerHTML=d;
</script>

</body>
</html>

So there you have it. The JSON notation for a date type needs to be simply:


jsondate = '{ "dateobj" : new Date("2012-07-22T19:49:00") }'

Epilogue

It turns out at least newer versions of Firefox JavaScript also support a newer Date.toJSON() function that will return a string like 2012-07-22T16:49:00.000Z. How this is supposed to be JSON I don't really know. If I would put that in the place of a value in a json document, it is not valid JavaScript syntax. If I put it into quotes, it of course becomes a string.

But the new Date(...) approach actually works.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • Allowed HTML tags: <h1> <h2> <h3> <h4> <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <sup> <div> <blockquote> <pre> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically. (Better URL filter.)

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.