在 nvd3.js 中格式化日期 [英] formatting the date in nvd3.js

查看:18
本文介绍了在 nvd3.js 中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I make the date format in nvd3.js. For example:

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

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

1374561814000 what does it mean, how its converted from a date?

推荐答案

日期 1374561814000 当前为 Unix 时间戳.

The date 1374561814000 is currently Unix Time Stamp.

您可以定义希望日期在传递到图表时的显示方式.阅读 d3 指南以了解 时间格式它会让你更好地理解.

You can define how you want the date to be displayed in your when passed into your chart. Have a read on the d3 guide to Time formatting it will give you a better understanding.

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

或者您可能想要返回显示日期/月份/年份的日期戳,因为您可以简单地这样做:

Or you may want to return the date stamp showing the date/month/year, for that you could simply do:

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

假设您希望 unix 时间戳将日期返回为时间(以分钟和小时为单位):

Lets say you want the unix time stamp to return the date as Time in miniutes and hours hours it would simply be :

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

以上示例已经过测试HERE,尝试使用以下值(取自 d3 时间格式文档).

The above examples have been tested HERE, have a play around using the values below(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.

希望对你有帮助.

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

这篇关于在 nvd3.js 中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆