D3时间解析返回null [英] 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"));
推荐答案
您必须添加您提供的所有字段到格式字符串。
You have to add all the fields you're supplying to the format string.
与自然语言日期解析器(包括JavaScript的内置解析)不同,此方法是严格的:如果指定的字符串不正确匹配关联的格式说明符,此方法返回null。例如,如果关联的格式是完整的ISO 8601字符串%Y-%m-%dT%H:%M:%SZ,那么将正确解析字符串2011-07-01T19:15:28Z ,但2011-07-01T19:15:28,2011-07-01 19:15:28和2011-07-01将返回null,尽管有效的8601日期。
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.
尝试以下操作:
var format = d3.time.format("%Y-%m-%dT%H:%M:%SZ");
alert(format.parse("2011-07-01T19:15:28Z"));
在指定的时间和日期创建一个新的Date对象。
That creates a new Date object at the time and date specified.
这篇关于D3时间解析返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!