简体   繁体   中英

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. 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 . 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.

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 ?

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 . 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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