Alphablony
Alphablony

Reputation: 33

Angular 9 timezone DatePipe

I need to display date in specific timezone.

In my app.module I provide locale_id in french and date is display in good format DD/MM/YYYY.

But I get the date time in UTC Format, and I want to display date in user's timezone.

I work on french PC. If I make this code :

let offset = new Date().getTimezoneOffset();
console.log(offset);
// I get -120

But this :

{{gettingDate | date: 'short'}} 
// this display date time in UTC 

How to display for all date in my application the date in good user's timezone not in hard-code

Upvotes: 0

Views: 5447

Answers (1)

bik
bik

Reputation: 339

Basically the best is to get the local browser language and tell Angular in which format you want to display your date the LOCALE_ID help you to do this so in your app module you need :

import {LOCALE_ID} from '@angular/core';

const localeLang: string = () => {
   const userLang = navigator.language || navigator.userLanguage;
return userLang.split('_')[0];  // example en_US -> en
};

in your providers app module

    providers: [
        {
            provide: LOCALE_ID,
            useFactory: localeLang
        },
]

for more details https://angular.io/api/core/LOCALE_ID

Upvotes: 1

Related Questions