How to get timezone with getDatePattern


#1

I’m trying to get the timezone with getDatePattern,
I have a provider, this is the code that I have in it:

import { HttpClient } from ‘@angular/common/http’;
import { Injectable } from ‘@angular/core’;
import { Globalization } from ‘@ionic-native/globalization’;

@Injectable()
export class TimezoneProvider {
public segundos :any;

constructor(public globalization: Globalization) {}

getTimeZone(){

this.globalization.getDatePattern (  
  { formatLength: 'short', selector: 'date and time'} )
  
  .then(res => {
     let seconds = res.utc_offset;
      this.segundos = seconds;
  });

  return this.segundos

}
}

in the page where I using the provider,
the variable is shown undefined

segundosTimeZone;
constructor(public navCtrl: NavController, public alertCtrl: AlertController, public timeZone:TimezoneProvider) {

      this.segundosTimeZone = this.timeZone.getTimeZone();
      console.log(this.segundosTimeZone);
}

Thanks!

#2

Recommend to use moment.js.
let offset = moment().utcOffset();

It has ton of useful time/date features ( plus, super simple ).


#3

Thank you so much!!! It Works.