简体   繁体   English

怎么把Moment转换成D3格式?

[英]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. 在Node中,我使用Moment作为时间,而在前端中,我使用D3软件包。 For moment, I will get a time of 1429588800 which is converts to "Tue Apr 21 2015 00:00:00 GMT-0400". 暂时,我将获得时间1429588800,将其转换为“ 2015年4月21日星期二00:00:00 GMT-0400”。 But in D3, I get "05/031976". 但是在D3中,我得到“ 05/031976”。 This is with the format "%x". 格式为“%x”。

The problem I believe is that you use seconds ( unix timestamp ). 我相信的问题是您使用秒( unix timestamp )。 If you multiply it by 1000 it should work. 如果将其乘以1000,它应该可以工作。 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

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

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