-2

I have three date formats displayed below :-

en-GB   –             DD/MM/YYYY hh:mm:ss
en-US   –             MM/DD/ YYYY hh:mm:ss
zh-cn || zh –         YYYY/MM/DD hh:mm:ss

I have to develop an angular pipe which actually detects the locale / Browser language or language from the localstorage which i stored it should format the date according to the language of the application

i.e

if language is en-GB then it should format the date in --> DD/MM/YYYY hh:mm:ss
or 
if language is en-US then it should format the date in -->  MM/DD/ YYYY hh:mm:ss
or
if language is zh-cn || zh then it should format the date in -->  YYYY/MM/DD hh:mm:ss

Please any code referral or any link it would be an ultimate help from you people.

thank's

Mayank Purohit
  • 163
  • 1
  • 4
  • 17

1 Answers1

0

according to your requirement you can use DatePipe service which you can import from @angular/common lib

here is the example

import { DatePipe } from "@angular/common";

in your component just add into your @Component decoretor

 providers: [DatePipe]

and

gb = new Date();
  zh = new Date();
  us = new Date();

constructor(private datePipe: DatePipe) {
    this.gb = this.datePipe.transform(this.gb, "dd/MM/yyyy hh:mm:ss");
    this.us = this.datePipe.transform(this.us, "MM/dd/yyyy hh:mm:ss");
    this.zh = this.datePipe.transform(this.zh, "yyyy/MM/dd hh:mm:ss");
 }

here you should write constructor code according to your localstorage value what you are getting. and I hope you are getting language from somewhere(localstorage) or you have stored it. if you are not getting language of browser than you can get it click on below link it will help you to get browser language

How to get the browser language using JavaScript

after it below link also will help you Angular - Use pipes in services and components

hope this would help you, let me know if you need any help regarding it.

thanks, Kushal

kushal shah
  • 823
  • 6
  • 9