Get the timezone from my browser/server to inject in angular2-highcharts

Emile Cantero picture Emile Cantero · Jul 19, 2017 · Viewed 9.7k times · Source

I actually get the timezone setup manually using the timezoneOffset fonction comming from the Highcharts api, I am currently in gmt+2 so I set it to -2 * 60 but when we ago change the hour in October my setup will not work propely anymore, so I decide to take my browser or the servor time instead. I know we could take the gettimezoneOffset function comming from the api getTimezoneOffset: Function. The pb is I am setup with typescript and Angular 4, how could I make it in an elegant way? thanks in advance

Here's my actual working code using 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();
}
    }

Here's my html:

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

Answer

Jaimin Raval picture Jaimin Raval · Jul 19, 2017

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

But I think you should get the time zone from the server because browser give the timezone according to local system, if someone changes it it will cause problem for you.