2016-01-13 181 views
1

这似乎是一个微不足道的问题,但我得到一个奇怪的错误:大纪元转换与D3

"Uncaught TypeError: t.slice is not a function"

试图Unix时间戳传递给我的代码时。

var myDate = new Date(1075611600000); 
    var format = d3.time.format("%Y").parse; //Trying to return the year. 
    console.log(format(myDate)); //error noted above 
    console.log(myDate); //Sun Feb 01 2004 00:00:00 GMT-0500 (EST) 

有人看到我正在犯的明显错误吗?提前致谢。我看过this的例子,我不明白我做错了什么。我试图只返回一年。

+0

你到底想实现什么呢? – Bigood

回答

3

format函数需要一个字符串,并且您传入一个Date对象,该对象不具有slice函数。

我相信这是你想要达到的目标:

var format = d3.time.format("%Y-%m-%d"); 
console.log(format(new Date(1075611600000))); // returns a string 

[jsfiddle]

https://github.com/mbostock/d3/wiki/Time-Formatting

+0

不错,谢谢! –

0

你传递一个Dateparse方法,where you should pass it a string.

如果你想从中获得,don't use .parse()


PS:您使用的精缩D3,所以Uncaught TypeError: t.slice is not a function代表:

Uncaught TypeError: string.slice is not a function 

d3_time_parseFullYear @ d3.js:2760 
d3_time_parse @ d3.js:2563 
format.parse @ d3.js:2538