json日期格式转Highcharts日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14668729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:43:45  来源:igfitidea点击:

json date format to Highcharts date format

jsondatetimehighcharts

提问by agri

Could please someone guide me how to get such json object datetime

请有人指导我如何获得这样的json对象日期时间

 {
    value="01/01/2013 08:00", 
    key=5.5
 }

to javascript (to use in Highcharts) datetime acceptable format

到 javascript(在 Highcharts 中使用)日期时间可接受的格式

[Date.UTC(2013, 0, 1,08,00), 5.5].

UPDATE

更新

Here is what I'm trying to do:

这是我想要做的:

var chart;

$(document).ready(function () {

    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            zoomType: 'x'
        },
        yAxis: {
            type: 'double',
            min: 0
        },
        xAxis: {
            type: 'datetime',
            labels: {
                formatter: function () {
                    return Highcharts.dateFormat('%a %d %b %H:%M', this.value);
                },
                dateTimeLabelFormats: {
                    minute: '%H:%M',
                    hour: '%H:%M',
                    day: '%e. %b',
                    week: '%e. %b',
                    month: '%b \'%y',
                    year: '%Y'
                }
            }
        },
        series: [{
            name: 'MyData1',
            data: []
        }, {
            name: 'MyData2',
            data: []
        }]

    });

    chart.series[0].setData([
        [Date.UTC(2013, 0, 1, 08, 00), 4.4],
        [Date.UTC(2013, 0, 1, 12, 00), 6.0],
        [Date.UTC(2013, 0, 1, 17, 00), 7.7],
        [Date.UTC(2013, 0, 1, 22, 00), 5.8]
    ]);
    chart.series[1].setData([
        [Date.UTC(2013, 0, 1, 08, 30), 0.4],
        [Date.UTC(2013, 0, 1, 10, 00), 0.0],
        [Date.UTC(2013, 0, 1, 16, 00), 0.7],
        [Date.UTC(2013, 0, 1, 20, 00), 0.8]
    ]);
});

hereis it in jsFiddle.

是在 jsFiddle 中。

var datatype1=[];
var datatype2= [];

    for(index in userReadingsJsonCollection.items){
          if(userReadingsJsonCollection.items[index].ReadingsData.Reading_Type==type1){
                datatype1.push( 
[Date.parse(userReadingsJsonCollection.items[index].ReadingsData.Rec_DateTime),
                userReadingsJsonCollection.items[index].ReadingsData.Reading]
                );
            }
          if(userReadingsJsonCollection.items[index].ReadingsData.Reading_Type==type2){
                datatype2.push(
                [userReadingsJsonCollection.items[index].ReadingsData.Rec_DateTime,
                userReadingsJsonCollection.items[index].ReadingsData.Reading]              
                );
            }
    }

The result I get is [[1357027200000,5.5]] and this works great.

我得到的结果是 [[1357027200000,5.5]] 并且效果很好。

回答by Mark

If your time is already UTC then it's as simple as:

如果您的时间已经是 UTC,那么它很简单:

Date.parse("01/01/2013 08:00")

EDITS

编辑

Assuming your data comes back as valid JSON:

假设您的数据作为有效的 JSON 返回:

var jsonObj = {value:"01/01/2013 08:00", key:5.5}; 

var seriesOneData = [];
seriesOneData.push([Date.parse(jsonObj.value),jsonObj.key]);