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"));
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With