How to get exact Time zone using date-fns?

Viewed 630

I am using the following date pipe for formatting date using date-fns library

date.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import { format } from 'date-fns';

@Pipe({
  name: 'formatDate'
})
export class FormatDatePipe implements PipeTransform {
  transform(value: string | number | Date, dateFormat: string): string {
    return format(value, dateFormat);
  }
}

component.html

<h1> {{ startDate | formatDate: 'DD-MM-YYYY HH:mm:ss.SSSZ' }} </h1>

I am getting the following output

output

01-01-2021 07:09:00.000-05:00 

but I am trying to get the exact time zone instead of just -05:00, for example, in this case (UTC-5) Expected output

01-01-2021 07:09:00.000 (UTC-5)
1 Answers

You need to use OOOO as mentioned at date-fns format documentation page.

Apart from this, you have used some wrong letters e.g. you have used

  1. Y instead of y
  2. D instead of d

Demo:

import { format } from 'date-fns';

const now  = new Date();

console.log(format(now, "dd-MM-yyyy HH:mm:ss.SSSOOOO"));

Output from a sample run in my timezone, Europe/London:

07-08-2021 13:17:55.549GMT+01:00

ONLINE DEMO

(Note: Click Console on the bottom right of the ONLINE DEMO page.)

Related