date - 在 nvd3.js 中格式化日期

标签 date format nvd3.js

如何在 nvd3.js 中制作日期格式。例如:

data1 = [{
  "values": [{
    "x": 1374561814000,
    "y": 2
  }],
  "key": "x-axis"
}]
1374561814000 是什么意思,它是如何从日期转换而来的?

最佳答案

日期 1374561814000 当前是 Unix 时间戳。

您可以定义日期在传递到图表时的显示方式。阅读 Time formatting 的 d3 指南,它会让您更好地理解。

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 时间戳将日期返回为以分钟和小时为单位的时间,它只是:
return d3.time.format('%X')(new Date(d)) // Capital X

上面的例子已经过测试 HERE ,使用下面的值( taken from the d3 time formatting docs )玩一下。
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.

希望能帮助到你。

如果其他成员觉得这需要改进,请继续改进答案。

关于date - 在 nvd3.js 中格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18072721/

相关文章:

java - 如何计算饼图中显示的日期与当前日期之间的差异

javascript - 如何计算给定日期是今天还是昨天?

python - 在标记自定义迷你格式时正确考虑多个反斜杠

javascript - 插值 nvd3 时 LineWithFocusChart 溢出

d3.js - nvd3.jsstackedAreaChart 图表转换。就像在 nvd3.org 上一样

c++ - 如何在 Windows 上解析由 std::put_time ("%x") 创建的字符串?

javascript - AngularStrap 数据开始日期不允许选择日期

sql-server - 为什么 SQL Server CONVERT of a DATE value to a date+time style 不包括时间?

emacs - 如何在 Emacs 组织模式中插入带有日期和时间的日程表

javascript - 如果没有,则需要在 nvd3 multiBarHorizo​​ntalChart 中垂直滚动。酒吧数量增加