Tuesday, 18 July 2023

Dayjs (and other browser based date libraries) returning wrong offsets when local browser time zone is changed

I'm seeing some strange behavior that I don't really understand what is happening. I'm using Dayjs as a library and want to display user data based on the time zone that the user has specified on my app. In the below example, the user may be in America/Chicago, but the user specified Africa/Accra as their time zone, so all their data in my app will be in the Africa/Accra time zone.

Great, I simply use:

dayjs.tz(1234567890, "Africa/Accra")

Afterward, I can always do something like dayjs.tz(1234567890, "Africa/Accra").offsetName() to get the name of the time zone (in this case Africa/Accra, which is a GMT or GMT+0 offset.

However, when I install a Chrome plugin to change my Chrome browser time zone (for example, https://chrome.google.com/webstore/detail/change-timezone-for-googl/cejckpjfigehbeecbbfhangbknpidcnh), and change to any time zone from the list, dayjs returns wrong time zone offset names.

It returns exclusively time zone offsets of the time zone specified by the browsere plugin. It completely ignores the time zone that I specify in the dayjs object.

For example, if I use that plugin to change the browser time zone to America/Nassau, dayjs.tz(1234567890, "Africa/Accra").offsetName() will return GMT+7, which is wrong. Refreshing the page to re-run the offsetName method will return a different offset name every time, so it's not even consistent.

I was under the assumption that dayjs will return the time zone offset that is inputted in the dayjs.tz function...which it always does. However, when I use this browser plugin, it doesn't and I do not know why.

Does anyone have any input? This behavior seems worrying to me and it's probably because I don't know what's going on.



from Dayjs (and other browser based date libraries) returning wrong offsets when local browser time zone is changed

No comments:

Post a Comment