2016-01-13 115 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