0

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

Alphablony
  • 23
  • 1
  • 4

1 Answers1

1

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

bik
  • 319
  • 1
  • 8
  • Ok for the format of the date like DD/MM/YYYY in french or MM/dd/yyyy for US . but not for the timezone. `{{ value_expression | date [ : format [ : timezone [ : locale ] ] ] }}` My problem isn't in format part is in the timezone part of the Pipe. – Alphablony Aug 03 '20 at 17:42
  • https://stackblitz.com/edit/angular-ivy-zl44vy?file=src%2Fapp%2Fapp.component.ts – bik Aug 03 '20 at 19:27
  • I hope that helps :) – bik Aug 03 '20 at 19:28