Highcharts:x值作为日期 [英] Highcharts: x-value as date

查看:339
本文介绍了Highcharts:x值作为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Highcharts有一个数组列表[x,y]。我的x值是格式 2013-04-30 00:04:00 的时间戳。



以下是Highchart选项的示例:

 系列:[ {
name:'2013-04-30',
data:[['2013-04-30 00:00:00',30],['2013-04-30 00:01: 00',32],['2013-04-30 00:02:00',40],['2013-04-30 00:03:00',21],['2013-04-30 00:04 :00',28]]
}]

我的时间戳可能会以不规则的间隔发生。

我意识到我无法做到这一点,时间戳是字符串,他们只是被解释为点的名字。我是否需要将时间戳转换为Date.UTC?这是唯一的方法吗?例如 2013-04-30 00:01:00 - > Date.UTC(2013,04,30,0,1,0)。我猜这将需要很多字符串连接Date.UTC(+ year +,+ month +,+ day,etc ... +)



试图弄清楚是否有更简单的方法来做到这一点......谢谢。

解决方案

这几乎是ISO日期字符串,看起来像2013-04-03T00:00:00和 Date(string)构造函数可以接受ISO字符串。



在Chrome中可以使用。 (如果你在结尾添加了Z,那么它被认为是UTC时间,如果你把它关掉,它将被当作本地时间,如果你需要的话,可以在不同的时区添加字符串。)

  var d ='2013-04-30 00:00:00Z'; 
var date = new Date(d);

在FireFox和IE9中,您必须添加'T'。 (如2013-04-30T00:00:00Z)



所以你可以在这三种情况中使用它:

  var d ='2013-04-30 00:00:00'; 
var date = new Date(d.replace('','T')+'Z')

这是一个小提琴: FIDDLE HERE


I have a list of arrays as [x,y] for Highcharts. My x-values are timestamps in the format 2013-04-30 00:04:00.

Here's an example of the Highchart options:

series: [{
            name: '2013-04-30',
            data: [['2013-04-30 00:00:00', 30], ['2013-04-30 00:01:00', 32], ['2013-04-30 00:02:00', 40], ['2013-04-30 00:03:00', 21], ['2013-04-30 00:04:00', 28]]
}]

My timestamps may occur at irregular intervals.

I realized I couldn't do this bc the timestamps are strings, and they simply get interpreted as the name of the point. Do I need to convert the timestamps into Date.UTC? Is this the only way? e.g. 2013-04-30 00:01:00 -> Date.UTC(2013,04,30,0,1,0). I'm guessing this will take a lot of string concatenation with "Date.UTC(" + year +"," + month + "," + day, etc... + ")"

Just trying to figure out if there is an easier way to do this...Thanks.

解决方案

Those are almost ISO date strings which would look like "2013-04-03T00:00:00" and the Date(string) constructor can take ISO strings.

In Chrome this works. (If you add the Z on the end, its taken as UTC time. If you leave it off, its taken as local time. There are strings to append for the various time zones if you need that.)

var d = '2013-04-30 00:00:00Z';
var date = new Date(d);

In FireFox and IE9 you have to add the 'T'. (as in 2013-04-30T00:00:00Z)

So you can use this in all three:

var d = '2013-04-30 00:00:00';
var date = new Date(d.replace(' ', 'T') + 'Z')

Here's a fiddle: FIDDLE HERE

这篇关于Highcharts:x值作为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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