2

我实际上使用timezoneOffset来自 Highcharts api 的函数手动设置时区,我目前在 gmt+2 所以我将它设置为 -2 * 60 但是当我们之前在 10 月更改小时时,我的设置将不再正常工作,所以我决定改用浏览器或服务器时间。我知道我们可以使用来自 api getTimezoneOffset: Function的 gettimezoneOffset 函数。pb 是我使用 typescript 和 Angular 4 设置的,我怎样才能以一种优雅的方式进行设置?提前致谢

这是我使用 timezoneOffset 的实际工作代码:

 constructor(public userService3: UserService3) {

     const Highcharts = require('highcharts');

    Highcharts.setOptions({
  global: {
    timezoneOffset: -2 * 60
   }
});
           this.options = {
            title : { text : '' },
            chart: {  type: 'area'},
            legend: { enabled: false },
            credits: { enabled: false },
            xAxis: { type: 'datetime',
                    startOnTick: false,
                    endOnTick: false,
                    tickInterval: 36e5 * 2, // two hours
                    },
            yAxis: { min: 0,
              max: 100 },
            plotOptions: {
              series: {
                  color: '#648e59',
                  fillOpacity: 0.8,
                  fillColor: '#648e59',
                  pointInterval: 36e5 * 2 // two hours
                      }
            },
            series: [{
              name: 'Someone1',
              data: [],
            }]
        };
    }
   saveInstance(chartInstance) {
    this.chart = chartInstance;
     console.log(chartInstance);
}

    public ngOnInit () {
    this.dataSubscription = this.userService3.getData().subscribe((data) 
=> {
      this.options.series[0].data = data.data.operating_details;
      console.log(data);
   });
}
    ngOnDestroy() {
      if (this.dataSubscription){
this.dataSubscription.unsubscribe();
}
    }

这是我的html:

   <chart [options]="options" (load)="saveInstance($event.context)"> 
   </chart>
4

2 回答 2

4

var date = new Date(); var timezone = date.getTimezoneOffset();

但我认为你应该从服务器获取时区,因为浏览器根据本地系统给出时区,如果有人更改它会给你带来问题。

于 2017-07-19T13:18:07.393 回答
1

您可以简单地从您的客户端获取时区偏移量:

const timeZoneOffset = new Date().getTimezoneOffset();

您将在几分钟内获得与 UTC 的本地差异,然后根据需要使用它。根据您的应用程序类型,对于大多数情况下,当您的客户端位于不同时区时,这应该是比硬编码时区更好的方法。

于 2017-07-19T12:59:51.037 回答