javascript d3.js日期字符串解析问题 [英] 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);
});
但在我的 forEach
对象每次返回null。我不明白为什么会失败。
yet in my forEach
method, my date object returns null every time. I don't understand why this fails.
同样
{date: 2013-03-04 00:00:00 + 0000}
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
为什么我的字符串日期解析器失败与我的第一个方案?我尝试在chrome中逐步通过控制台/ inspect元素中的日期解析器,但无效。
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.
这里是d3.time.format文档 https://github.com/mbostock/d3/wiki/Time-Formatting
here are the d3.time.format documents https://github.com/mbostock/d3/wiki/Time-Formatting
推荐答案
从链接到的文档页面,进一步向下:
From the documentation page that you link to, further down:
以下指令尚不支持解析:
The following directives are not yet supported for parsing:
%j - 一年中的日期。
%j - day of the year.
%U - 一年的周数。
%U - week number of the year.
%w - 工作日数。
%w - weekday number.
%W - 周
%。
%Z - 时区偏移,例如-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.
如果你在GMT的所有东西都没问题,只需替换 + %Z
与您的格式规范中的 +0000
。否则,您必须找到另一种解析日期的方法。
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 d3.js日期字符串解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!