CQM
CQM

Reputation: 44278

javascript d3.js date string parsing issue

assume this is my string in my data object

{ date: "2013-03-04 12:00:00+0000" }

var parseDate = d3.time.format("%Y-%m-%d_%H:%M:%S+%Z");

data.forEach(function(d) {
            d.date = parseDate(d.date);
        });

yet in my forEach method, my date object returns null every time. I don't understand why this fails.

Similarly

{ date: "2013-03-04 00:00:00+0000" }

with

d3.time.format("%Y-%m-%d 00:00:00+00").parse

works fine. But the date is less precise because it assumes everything happens at midnight in my visualization

Why is my string date parser failing with my first scenario? I tried stepping through the date parser in console/inspect element in chrome, but to no avail.

here are the d3.time.format documents https://github.com/mbostock/d3/wiki/Time-Formatting

Upvotes: 2

Views: 6134

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109282

From the documentation page that you link to, further down:

The following directives are not yet supported for parsing:

%j - day of the year.

%U - week number of the year.

%w - weekday number.

%W - week number of the year.

%Z - time zone offset, such as "-0700".

%% - a literal "%" character.

The time zone offset you're trying to parse is not supported. The second one works because you're not trying to parse that.

If you're fine with everything being in GMT, just replace the +%Z with +0000 in your format specification. Otherwise, you'll have to find another way of parsing dates.

Upvotes: 4

Related Questions