Javascript Highcharts 上的时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12052671/
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-08-23 07:00:20  来源:igfitidea点击:

Timezone on Highcharts

javascripthighchartstimezone

提问by Codey McCodeface

As far as I understand the default for Highcharts is to UTC. I have tried to do as suggested by the answer in this post Highcharts graph X-axis label for different date ranges. If I understand correctly this should set the timezone to be that of the browser.

据我了解,Highcharts 的默认值是 UTC。我已尝试按照这篇文章Highcharts graph X-axis label for different date range 中的答案进行操作。如果我理解正确,这应该将时区设置为浏览器的时区。

I've tested this on jsFiddle and toggling the useUTCoption seems to have no effect.

我已经在 jsFiddle 上对此进行了测试,并且切换该useUTC选项似乎没有效果。

http://jsfiddle.net/looneyp/me3ry/

http://jsfiddle.net/looneyp/me3ry/

Question: What am I doing wrong above and how do you set the time zone correctly?

问题:我在上面做错了什么,你如何正确设置时区?

采纳答案by Codey McCodeface

It's one of those days

这是那些日子之一

I'm in the UK so UTC true or false gives the same results since I am in GMT. My discrepancy between my expected displayed time was due to a parsing issue in PHP to unixTime.

我在英国,所以 UTC true 或 false 给出了相同的结果,因为我在格林威治标准时间。我的预期显示时间之间的差异是由于 PHP 到 unixTime 的解析问题。

回答by magagnon

Highcharts now supports timezones in 3.0.8

Highcharts 现在支持 3.0.8 中的时区

You can now set the timezoneOffset global property: http://api.highcharts.com/highcharts#global.timezoneOffset

您现在可以设置 timezoneOffset 全局属性:http: //api.highcharts.com/highcharts#global.timezoneOffset

回答by Dennis

Nothing you are doing seems to be wrong. This jsFiddleshows a shift in the x-axis when UTC is disabled in the global Highcharts options.

你所做的一切似乎都没有错。 当在全局 Highcharts 选项中禁用 UTC 时,此 jsFiddle显示 x 轴的偏移。

回答by Allan Houston

Highcharts have moved this option in more recent versions.

Highcharts 在最近的版本中移动了这个选项。

One thing to keep in mind is that your shift seems to be the inverse or negative of where your local time from UTC, so if you're in UTC+2 for example, then your timezoneOffsetis going to be -2 * 60, which is not entirely intuitive:

要记住的一件事是,您的转变似乎与本地时间与 UTC 的位置相反或负值,因此,例如,如果您在 UTC+2,那么您timezoneOffset将是-2 * 60,这并不完全直观:

Highcharts.setOptions({
    time: {
        timezoneOffset: -2 * 60
   }
});

API documentation: https://api.highcharts.com/highcharts/time.getTimezoneOffset

API 文档:https: //api.highcharts.com/highcharts/time.getTimezoneOffset

Here is an example of it: http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/time/timezoneoffset/

这是一个例子:http: //jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/time/timezoneoffset/

回答by veritas

Add this code to the document.ready and you should get the highcharts in your local timezone if you're feeding it UTC time

将此代码添加到 document.ready 中,如果您使用 UTC 时间,您应该在本地时区获取高图

$(document).ready(function () {

  Highcharts.setOptions({
      global: {
          /**
           * Use moment-timezone.js to return the timezone offset for individual
           * timestamps, used in the X axis labels and the tooltip header.
           */
          getTimezoneOffset: function (timestamp) {
              d = new Date();
              timezoneOffset =  d.getTimezoneOffset()

              return timezoneOffset;
          }
      }
  });

});

回答by Joe Sebin

use timezone by getfunction Example: Using getTimezoneOffset

通过 getfunction 使用时区 示例:使用 getTimezoneOffset

var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;