首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nvd3.js中格式化日期

在nvd3.js中格式化日期
EN

Stack Overflow用户
提问于 2013-08-06 05:59:34
回答 1查看 11.2K关注 0票数 5

如何在nvd3.js中生成日期格式。例如:

代码语言:javascript
复制
data1 = [{
  "values": [{
    "x": 1374561814000,
    "y": 2
  }],
  "key": "x-axis"
}]

1374561814000它是什么意思,它是如何从一个日期转换的?

EN

回答 1

Stack Overflow用户

发布于 2013-08-06 08:54:41

日期1374561814000目前是。

您可以定义希望将日期显示在何时传递到图表中的日期。阅读一下d3到Time formatting的指南,它将给您一个更好的理解。

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

或者您可能希望返回显示日期/月/年的日期戳,因为您可以这样做:

代码语言:javascript
复制
return d3.time.format('%d/%m/%y')(new Date(d))

假设您希望unix时间戳将日期作为时间(以小时为单位)返回,它只是:

代码语言:javascript
复制
return d3.time.format('%X')(new Date(d)) // Capital X

上面的例子已经测试了这里,使用下面的值(https://github.com/mbostock/d3/wiki/Time-Formatting)进行了一次尝试。

代码语言:javascript
复制
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.

希望能帮上忙。

如果其他议员认为这方面需要改善,请继续改善答案。

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18072721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档