2013-08-06 68 views
4

如何在nvd3.js中設置日期格式。例如:在nvd3.js中格式化日期

data1 = [{ 
    "values": [{ 
    "x": 1374561814000, 
    "y": 2 
    }], 
    "key": "x-axis" 
}] 

1374561814000這是什麼意思,它是如何從日期轉換?

回答

12

日期1374561814000是當前的UNIX時間戳。

您可以定義如何將日期顯示在傳遞到圖表中時的顯示方式。閱讀d3指南Time formatting它會給你一個更好的理解。

chart.xAxis.tickFormat(function(d) { 
    // Will Return the date, as "%m/%d/%Y"(08/06/13) 
    return d3.time.format('%x')(new Date(d)) 
}); 

或者你會想返回顯示日期/月/年的日期戳,對,你可以簡單地做:

return d3.time.format('%d/%m/%y')(new Date(d)) 

比方說,你想要的UNIX時間戳返回日期如時間miniutes和小時小時,將簡單地是:

return d3.time.format('%X')(new Date(d)) // Capital X 

上述實施例已經測試HERE,具有圍繞利用下面(的值的播放)。

Constructs a new local time formatter using the given specifier. The specifier string may contain the following directives.  

- %a - abbreviated weekday name. 
- %A - full weekday name. 
- %b - abbreviated month name. 
- %B - full month name. 
- %c - date and time, as "%a %b %e %H:%M:%S %Y". 
- %d - zero-padded day of the month as a decimal number [01,31]. 
- %e - space-padded day of the month as a decimal number [ 1,31]; equivalent to %_d. 
- %H - hour (24-hour clock) as a decimal number [00,23]. 
- %I - hour (12-hour clock) as a decimal number [01,12]. 
- %j - day of the year as a decimal number [001,366]. 
- %m - month as a decimal number [01,12]. 
- %M - minute as a decimal number [00,59]. 
- %L - milliseconds as a decimal number [000, 999]. 
- %p - either AM or PM. 
- %S - second as a decimal number [00,61]. 
- %U - week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. 
- %w - weekday as a decimal number [0(Sunday),6]. 
- %W - week number of the year (Monday as the first day of the week) as a decimal number [00,53]. 
- %x - date, as "%m/%d/%Y". 
- %X - time, as "%H:%M:%S". 
- %y - year without century as a decimal number [00,99]. 
- %Y - year with century as a decimal number. 
- %Z - time zone offset, such as "-0700". 
- %% - a literal "%" character. 

希望它有幫助。

如果其他成員覺得需要改進,請繼續並改進答案。

+0

我明白了。謝謝您的幫助。在x軸中,日期以d/m/y格式顯示。但是當我試圖將鼠標懸停在特定的座標上時,其顯示NaN/NaN/NaN。可能是什麼問題? – Mausumi