How to get timezone abbreviation with date-fns-tz?

196 Views Asked by At

I have an ISO string in UTC from my backend and I want to format it to particular timezone so I am using date-fns-tz for that as recommended in the docs

But when I try to print the date with timezone abbreviation token, I get the GMT+/-x instead of the timezone abbreviation instead

import formatInTimeZone from 'date-fns-tz/formatInTimeZone';
...

const timezone = "Asia/Calcutta";
const formattedDate = formatInTimeZone(parsedStartDate, timezone, 'dd MMMM yyyy hh:mm a z');
// fomattedDate -> 04 June 2023 09:00 AM GMT+5:30

As recommended by docs I also tried adding locale, and when I add a locale it works for the timezones that belong to that locale but doesn't work for others. For instance, if I add en-IN locale to the previous example it shows IST correctly but it still shows GMT+/-x for other European/American timezones

import formatInTimeZone from 'date-fns-tz/formatInTimeZone';
import enIN from 'date-fns/locale/en-IN';
...

const timezone = "Asia/Calcutta";
const formattedDate = formatInTimeZone(parsedStartDate, timezone, 'dd MMMM yyyy hh:mm a z'  { locale: enIN });
// fomattedDate -> 04 June 2023 09:00 AM IST

I really don't understand why it works this way, and want to know if I am missing something

0

There are 0 best solutions below