Highchart UTC日期始终设置为1970年1月1日

Highchart UTC date always set to January, 1 1970

本文关键字:1970年 1月 1日 设置 UTC 日期 Highchart      更新时间:2023-09-26

我从外部服务收到一个已转换为UTC的日期。Highcharts工具提示显示Jan 01, 1970我不确定为什么它没有正确解释日期。如果我手动将UTC时间转换为字符串,然后使用Date.UTC JavaScript方法,它可以很好地工作。我不知道为什么UTC格式的日期不起作用。

    var weightChart;
    var weightData = [];
    var minWeight;
    var maxWeight;
    $(function () {
        var json = {"ibw":175,"max_weight":300,
"min_weight":175,
"weights":[{"date":1232521200,"weight":300},
{"date":1245218400,"weight":300},
{"date":1313042400,"weight":290},
{"date":1319522400,"weight":270},
{"date":1330498800,"weight":200},
{"date":1342591200,"weight":250},
{"date":1365141600,"weight":235}]};

                    minWeight = json.min_weight;
                    maxWeight = json.max_weight;
                    $.each(json.weights, function(i, item) {
                        weightData.push([item.date, item.weight]);
                    });
                    displayGraph();

    });
    function displayGraph() {
                //graph
                weightChart = new Highcharts.Chart({
                    chart: {
                        renderTo: 'container',
                        type: 'spline',
                        zoomType: 'xy',
                        height: 113
                    },
                    credits: {
                        enabled: false
                    },
                    title: {
                        text: ''
                    },
                    tooltip: {
                        xDateFormat: '%b %d, %Y',
                        headerFormat: 'Date: <b>{point.key}</b><br />',
                        pointFormat: 'Weight: <b>{point.y}</b>'
                    },
                    xAxis: {
                        type: 'datetime',                   
                        labels: {
                            enabled: false
                        }
                    },
                    yAxis: {
                        title: {
                            text: ''
                        },
                        plotLines: [{
                            color: '#FF0000',
                            width: 2,
                            value: 125
                        }],
                        min: minWeight,
                        max: maxWeight
                    },
                    series: [{
                        name: ['Weight'],
                        data: weightData
                    }],
                    exporting: {
                        enabled: false
                    },
                    legend: {
                        enabled: false
                    },
                    plotOptions: {
                        series: {
                            borderWidth: 0,
                            colorByPoint: false,
                            pointWidth: 12,
                            shadow: true
                        }
                    }
                });
            }

这是它的小提琴

看起来数据来自UNIX时间戳中的后端。Highcharts需要一个javascript时间,它是以毫秒为单位的UNIX时间。它显示1970年1月1日是"1232521200"的日期。把你的日期戳乘以1000,你就会得到合适的时间。现场演示。

var json = {
    "ibw": 175,
    "max_weight": 300,
    "min_weight": 175,
    "weights": [{
        "date": 1232521200000,
        "weight": 300
    }, {
        "date": 1245218400000,
        "weight": 300
    }, {
        "date": 1313042400000,
        "weight": 290
    }, {
        "date": 1319522400000,
        "weight": 270
    }, {
        "date": 1330498800000,
        "weight": 200
    }, {
        "date": 1342591200000,
        "weight": 250
    }, {
        "date": 1365141600000,
        "weight": 235
    }]
};

我建议您将服务器端的日期转换为字符串,并提供正确的日期格式,然后将数据返回为字符串,高图将以正确的格式显示。

例如,在服务器端,我有DateTime对象

DateTime日期=DateTime.Now

我将日期转换为正确的格式,并将其作为字符串日期返回。ToString("dd.MM.yyyy")