在D3.js问题中解析时间戳
我正在尝试解析d3.js
中的图中使用的时间戳,但我无法设法使其正常工作。时间戳的示例如下所示:
- 2022-02-17 09:52:37.000000
- 2022-02-17 09:54:00.000000
我用来进行解析的代码是:
`const dateParser = d3.timeFormat("%Y-%m-%d");
data.forEach(function(d) {
d.timestamp = dateParser(d.ts);
});`
其中d.ts
是时间戳
我目前获得的输出以下是:
0nan-- Nan-nan
提前谢谢
I am trying to parse a timestamp to use in a plot in d3.js
but I cannot manage to get it to work. Examples of the timestamp are shown below:
- 2022-02-17 09:52:37.000000
- 2022-02-17 09:54:00.000000
The code that I use to do the parsing is:
`const dateParser = d3.timeFormat("%Y-%m-%d");
data.forEach(function(d) {
d.timestamp = dateParser(d.ts);
});`
Where d.ts
is the timestamp
The output I get at the moment is under the form:
0NaN-NaN-NaN
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是您的日期字符串吗?如果是这样,您可以将代码更改为首先将字符串日期转换为日期对象,然后在其上调用DateParser:
Are your dates strings? If so you could change your code to first convert the string date to Date object and then call dateParser on it:
从某种意义上说,解析是 formatting 的反面注意您的指定器,这是不正确的)。
因此,您需要
d3.TimeParse()
。这是工作片段,查看浏览器的控制台,而不是摘要的片段:In a way, parsing is the opposite of formatting: when you parse you convert a string to a date, while formatting is converting the date to a readable string (also, pay attention to your specifier, it's incorrect).
Therefore, you want
d3.timeParse()
instead. Here is the working snippet, look at your browser's console, not the snippet's one: