date-fns-tz
date-fns-tz copied to clipboard
'Invalid time zone specified' when calling formatInTimeZone with time zone offset and time zone tokens
Given the following documentation (emphasis mine):
formatInTimeZone
This function takes a
Date
instance in the system's local time or an ISO8601 string, and an IANA time zone name or offset string.
I expect the following to work:
formatInTimeZone("1986-04-04T10:32:55.123Z", "+2", "dd.MM.yyyy HH:mm zzzz");
What actually happens:
RangeError: Invalid time zone specified: +2
at new DateTimeFormat (<anonymous>)
at getDTF (/home/runner/tmp-nodejs/node_modules/date-fns-tz/_lib/tzIntlTimeZoneName/index.js:36:10)
at tzIntlTimeZoneName (/home/runner/tmp-nodejs/node_modules/date-fns-tz/_lib/tzIntlTimeZoneName/index.js:14:13)
at Object.z (/home/runner/tmp-nodejs/node_modules/date-fns-tz/format/formatters/index.js:105:35)
at /home/runner/tmp-nodejs/node_modules/date-fns-tz/format/index.js:344:75
at Array.reduce (<anonymous>)
at format (/home/runner/tmp-nodejs/node_modules/date-fns-tz/format/index.js:337:25)
at formatInTimeZone (/home/runner/tmp-nodejs/node_modules/date-fns-tz/formatInTimeZone/index.js:41:30)
at Object.<anonymous> (/home/runner/tmp-nodejs/index.js:4:13)
To reproduce, run the code in this Replit Repl
After a little more research, it looks like maybe it's not possible to convert a numerical offset to a named offset
'+02:00'
should work.
Thanks for the reply, @marnusw
'+02:00'
doesn't work. I get the same error:
RangeError: Invalid time zone specified: +02:00
I updated the Repl to use all numerical offset variations and none work
I will take a look. If you have the time, a PR with failing unit tests will help me sort this out quicker.
Here you go! https://github.com/marnusw/date-fns-tz/pull/187
Hello, same issue with another time zone
const date = new Date();
formatInTimeZone(date, 'Eastern Standard Time', 'yyyy-MM-dd HH:mm:ss zzz');
@dancornilov you have to specify the IANA time zone name: America/New_York
Closing this, continued in #187.
Hey, we've been getting hundreds of Sentry errors related to this issue but are quite stuck on how to approach this. It seems like the time zone name is definitely in IANA.
Our largest customer group is in Iceland and we've been getting:
Invalid time zone specified: Atlantic/Reykjavik
But also for
- Europe/Lisbon
- Europe/Prague
- America/Mexico_City
- Africa/Johannesburg
- Asia/Katmandu
- Asia/Dubai
We're calling formatInTimezone based on the "company" timezone:
export const getCompanyTimezone = () => {
const company = getCompany();
const companyTimezone = company?.connections.location?.timeZone;
if (companyTimezone && isValidTimezone(companyTimezone)) {
return companyTimezone;
}
return getUserTimezone();
};
Here's the isValidTimezone
function that has been implemented just for this issue, without any luck.
const isValidTimezone = (tz: string) => {
if (!Intl?.DateTimeFormat().resolvedOptions().timeZone) {
// This one has never been reported
Sentry.captureException(new Error('Intl.DateTimeFormat().resolvedOptions().timeZone is undefined'), {
extra: {
tz,
resolvedOptions: JSON.stringify(Intl.DateTimeFormat().resolvedOptions())
}
});
return false;
}
try {
Intl.DateTimeFormat(undefined, { timeZone: tz });
return true;
} catch (ex) {
return false;
}
};
I'm not sure how to approach this so any help is much appreciated ❤️
Hey @gunnartorfis, did you end up solving this issue?