Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 time parse returns null

As per the instructions on this page: https://github.com/mbostock/d3/wiki/Time-Formatting I am trying to parse an ISO 8601 date for use in D3.js. My test is almost word for word out of the post, I can't get it to work for a full date time string:

var format = d3.time.format("%Y-%m-%d");
alert(format.parse("2011-07-01T19:15:28Z")); 
like image 584
greenafrican Avatar asked Oct 24 '13 21:10

greenafrican


2 Answers

You have to add all the fields you're supplying to the format string.

Unlike "natural language" date parsers (including JavaScript's built-in parse), this method is strict: if the specified string does not exactly match the associated format specifier, this method returns null. For example, if the associated format is the full ISO 8601 string "%Y-%m-%dT%H:%M:%SZ", then the string "2011-07-01T19:15:28Z" will be parsed correctly, but "2011-07-01T19:15:28", "2011-07-01 19:15:28" and "2011-07-01" will return null, despite being valid 8601 dates.

Try this:

var format = d3.time.format("%Y-%m-%dT%H:%M:%SZ");
alert(format.parse("2011-07-01T19:15:28Z"));

That creates a new Date object at the time and date specified.

D3 Time Format

like image 123
Bill the Lizard Avatar answered Oct 22 '22 14:10

Bill the Lizard


It is super strict, always check your case. eg:

format = d3.time.format("%m/%d/%y"); returns null

but:

format = d3.time.format("%m/%d/%Y"); gives valid results.

like image 3
Bhaskar Devgon Avatar answered Oct 22 '22 14:10

Bhaskar Devgon