简体   繁体   English

d3.js日期时间格式说明符未按预期工作

[英]d3.js date time format specifier not working as expected

I am using the below format specifier for dealing with datetime inputs in my d3.js force directed graph 我正在使用以下格式说明符来处理d3.js强制有向图中的日期时间输入

    var parseDate = d3.timeFormat("%Y-%m-%d %H:%M:%S")

Below is an example of how the datetime data is in my graph. 以下是日期时间数据在我的图形中的示例。 But

    console.log(parseDate('2015-1-7 13:45:54'))

gives the following output: 给出以下输出:

 0NaN-NaN-NaN NaN:NaN:NaN

Is there anything additional that needs to be done with the input? 输入是否还有其他需要完成的工作?

Link to the fiddle 链接到小提琴

What you might be looking for is d3.timeParse() 您可能正在寻找的是d3.timeParse()

var parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S")

console.log(parseDate('2015-1-7 13:45:54'))

I think you need timeParse or utcParse 我认为您需要timeParseutcParse

var parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S")

var parseDate = d3.utcParse("%Y-%m-%d %H:%M:%S")

Updated fiddle 更新的小提琴

You have to pass a Date object to the parseDate function: 您必须将Date对象传递给parseDate函数:

var parseDate = d3.timeFormat('%Y-%m-%d %H:%M:%S');

// method 1 (month is zero-based)
console.log(parseDate(new Date(2015, 0, 7, 13, 45, 54)));

// method 2
console.log(parseDate(new Date('2015-1-7 13:45:54')));

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM