[Fixed] Custom GMT format date pipe in Angular

Issue

I’m using angular default date pipe for formatting my date, using this code.

{{'27-04-2021 08:30:00' | utcDate | date: 'EE, d MMMM OOOO'}}

The result is Tue, 27 April GMT+05:30

I have to show the result as Tue, 27 April GMT+5:30

I have to format the GMT format from XX:XX to X:XX.

Thanks.

Solution

Define this method in your component.

  replaceZone = (val: string | null): string => {
    if (val == null) {
      return '';
    }
    return val.replace(/0(\d:\d{2})$/, '$1');
  }

Then call like this.

{{replaceZone('27-04-2021 08:30:00' | utcDate | date: 'EE, d MMMM OOOO')}}

Leave a Reply

(*) Required, Your email will not be published