1

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.

1 Answers1

1

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')}}
N.F.
  • 3,844
  • 3
  • 22
  • 53
  • its showing the same date format `Tue, 27 April GMT+05:30`. – Sadique Here Mar 29 '21 at 06:38
  • 1
    Try one 0 less `` date: 'EE, d MMMM OOO' `` for the shorthand writing (https://angular.io/api/common/DatePipe#custom-format-options) . If this doesn´t fit (because of the :30 addon), then i fear you will have to write your own formater. Or talk to the business responsibles and ask them if a non standard writing style is worth the effort of creating such a formater. As i like to say "Follow the standards or be ready to pay at least twice. Once for creating a special case, and once for maintenance") – JanRecker Mar 29 '21 at 06:47
  • Hi @JanRecker Its showing only hours. that's `Tue, 27 April GMT+5` – Sadique Here Mar 29 '21 at 06:49
  • I have updated my answer. Please try again. – N.F. Mar 29 '21 at 07:03
  • define `replaceZone` method inside custom pipe rather than call directly inside `{{ }}` interpolation, because, every change detection cycle interpolation calls replaceZone method and gives performance issue of app – Developer Mar 29 '21 at 08:00