简体   繁体   中英

How to convert time format from Moment to D3?

In Node, I am using Moment for time and on the front-end I am using the D3 package. For moment, I will get a time of 1429588800 which is converts to "Tue Apr 21 2015 00:00:00 GMT-0400". But in D3, I get "05/031976". This is with the format "%x".

The problem I believe is that you use seconds ( unix timestamp ). If you multiply it by 1000 it should work. Here are some tests for you

var value = 1429588800;

var f = d3.time.format("%x");
var date = new Date(value*1000);
var moment_date = moment.unix(value);

console.log('date',date); //date Tue Apr 21 2015 00:00:00 GMT-0400 (Eastern Daylight Time)
console.log('moment date',moment_date.format()); //moment date 2015-04-21T00:00:00-04:00
console.log('d3 + moment date',f(moment_date.toDate())); //d3 + moment date 04/21/15
console.log('d3 + date',f(date)); //d3 + date 04/21/15

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