admin管理员组

文章数量:1434922

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

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./mbostock/d3/wiki/Time-Formatting

Share Improve this question asked Mar 5, 2013 at 16:49 CQMCQM 44.4k77 gold badges230 silver badges370 bronze badges 2
  • You might not need the [+] in [+%Z]. Try ["%Y-%m-%d_%H:%M:%S%Z"] – Andrew Commented Mar 5, 2013 at 16:55
  • @Andrew this did not work either d3.time.format("%Y-%m-%d_%H:%M:%S%Z").parse – CQM Commented Mar 5, 2013 at 17:22
Add a ment  | 

1 Answer 1

Reset to default 4

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.

本文标签: javascript d3js date string parsing issueStack Overflow