简体   繁体   English

如何在d3中获得更精确的秒数?

[英]How to get more precise seconds in d3?

I have some time data in database as unix timestamp, ie, 1446151150000. I want to parse it to be hr:min:sec format for d3. 我在数据库中有一些时间数据作为unix时间戳记,即1446151150000。我想将其解析为d3的hr:min:sec格式。 So i do it like this: 所以我这样做:

// d.localtime is unix timestamp
data.forEach(function(d) {
  d.localtime = parseDate(new Date(Number(d.localtime)).toString().substring(16, 24));
});

However I find timestamp 1446151150000 and 1446151150500 have exactly the same converted string 14:39:10 . 但是我发现时间戳14461511500001446151150500具有完全相同的转换字符串14:39:10 Namely, new Date(Number(1446151150000) and new Date(Number(1446151150500) are exactly the same as Thu Oct 29 2015 14:39:10 GMT-0600 (MDT) although the actual time has 0.5 second difference. 也就是说,尽管实际时间相差0.5秒,但new Date(Number(1446151150000)new Date(Number(1446151150500)Thu Oct 29 2015 14:39:10 GMT-0600 (MDT)完全相同。

Question, How can I get more precise second? 问题,如何获得更精确的秒数? ie, how to make the converted strings of 1446151150000 and 1446151150500 to be 14:39:10.00 and 14:39:10.50 seperately instead of 14:39:10 ? 即,如何使转换后的字符串14461511500001446151150500分别为14:39:10.0014:39:10.50而不是14:39:10

Thanks! 谢谢!

The precision is still there, you are just seeing your default locale string representation of the date. 精度仍然存在,您只是看到日期的默认区域设置字符串表示形式。 With d3, you can tweak the string formatting using a custom time format . 使用d3,您可以使用自定义时间格式来调整字符串格式 The specific one you are looking for is: 您要查找的特定对象是:

> dtFormat = d3.time.format("%a %b %e %Y %H:%M:%S.%L GMT%Z");
> dtFormat(new Date(1446151150500));
"Thu Oct 29 2015 16:39:10.500 GMT-0400"

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

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