d3时间格式返回null

d3 time format returning null

本文关键字:null 返回 格式 时间 d3      更新时间:2023-09-26

我有下面的代码,我正试图用d3.time.format()形成格式,例如,我在time变量中传递的日期是Thu Dec 24 04:59:54。根据文档,d3时间格式化程序是严格的,如果format()参数与要解析的时间不匹配,它将返回null,但据我所知,我以正确的格式传递,但仍然为null。

time = "Thu Dec 24 04:59:54";
parseTime = d3.time.format("%a%b%e%H:%M:%S");    
console.log("PARSE TIME" + parseTime.parse(time));

编辑:这是我正在使用的文档https://github.com/mbostock/d3/wiki/Time-Formatting#format

这是因为您的.format没有考虑空格,所以您需要将其更改为:

parseTime = d3.time.format("%a %b %e %H:%M:%S"); 

以你的榜样工作。或者,可以在应用.parse之前从time变量中删除空格。