dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

dayjs timezone is not work in expo app

Open SteveYuOWO opened this issue 9 months ago • 8 comments

  const a = dayjs(new Date(1384775720)).tz("America/Toronto").format("HH:mm:ss");
  const b = dayjs(new Date(1384775720)).tz("Asia/Shanghai").format("HH:mm:ss");

Put the above code into the nodejs running environment. It shows:

{ a: '19:39:35', b: '08:39:35' }

And put it into expo app(https://expo.dev/)

it shows:

00:39:35
Image

Here's my minimal reproduction https://github.com/SteveYuOWO/expo-dayjs-reproduction

SteveYuOWO avatar Apr 20 '25 23:04 SteveYuOWO

I just ripped my hair out for hours trying to figure out why it wasn't working

itajenglish avatar Apr 26 '25 00:04 itajenglish

I just ripped my hair out for hours trying to figure out why it wasn't working

So, is there a solution for this? How does the expo app using dayjs solve the time zone issue in general?

SteveYuOWO avatar Apr 26 '25 12:04 SteveYuOWO

I just ripped my hair out for hours trying to figure out why it wasn't working

So, is there a solution for this? How does the expo app using dayjs solve the time zone issue in general?

Seems like this is a known issues:

  • https://github.com/iamkun/dayjs/pull/2843
  • https://github.com/iamkun/dayjs/issues/1377

I'm going to switch to use a different approach for handling dates and timezones for now

itajenglish avatar Apr 26 '25 15:04 itajenglish

@itajenglish

dayjs currently has no solution. I use a native function to wrap it as a temporary solution.

https://github.com/SteveYuOWO/expo-dayjs-reproduction/pull/1

SteveYuOWO avatar May 02 '25 06:05 SteveYuOWO

@itajenglish

dayjs currently has no solution. I use a native function to wrap it as a temporary solution.

https://github.com/SteveYuOWO/expo-dayjs-reproduction/pull/1

Nice! I ended up replacing dayjs with spacetime

itajenglish avatar May 03 '25 13:05 itajenglish

@itajenglish dayjs currently has no solution. I use a native function to wrap it as a temporary solution. SteveYuOWO/expo-dayjs-reproduction#1

Nice! I ended up replacing dayjs with spacetime

Cool. Thank you for recommending this open source repo for me. The solution looks better than mine.

SteveYuOWO avatar May 10 '25 17:05 SteveYuOWO

dayjs relies on Intl API and IANA db present in nodejs and browsers, but it seems like in RN we don't have it by default. I ended up using spacetime (thanks @itajenglish) lib for timezone conversions

alitsvin avatar Jul 04 '25 11:07 alitsvin

I have problems with this also. I finally use

  const deviceOffset = new Date().getTimezoneOffset();
  const dayjsUtc = dayjs.utc(myUtcTime);
  const dayjsLocal = dayjsUtc.subtract(deviceOffset, "minute");

FabrizioTorrico avatar Dec 03 '25 14:12 FabrizioTorrico