代码之家  ›  专栏  ›  技术社区  ›  Emile Cantero

从我的浏览器/服务器获取时区,以插入angular2 highcharts

  •  2
  • Emile Cantero  · 技术社区  · 7 年前

    timezoneOffset 从Highcharts api开始,我现在在gmt+2,所以我将其设置为-2*60,但当我们在10月份更改时间时,我的设置将不再正常工作,因此我决定使用我的浏览器或伺服时间。我知道我们可以从api中获取gettimezoneOffset函数 getTimezoneOffset: Function pb是我用typescript和Angular 4设置的,我如何才能以优雅的方式进行设置?提前感谢

     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();
    }
        }
    

       <chart [options]="options" (load)="saveInstance($event.context)"> 
       </chart>
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   Jaimin Raval    7 年前

    var timezone=date.getTimezoneOffset();

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

        2
  •  1
  •   BogdanC    7 年前

    您只需从客户端获取时区偏移:

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