marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.06k stars 116 forks source link

formatInTimeZone PPppp formatting unexpected GMT offset #210

Open duckRabbitPy opened 1 year ago

duckRabbitPy commented 1 year ago

Problem: When using the formatInTimeZone function passing in an ISOstring (Z included), 'UTC' as the timezone, and PPppp as the format the time returned is correct but there is an unexpected GMT offset in the returned string e.g. GMT+1 which changes depending on system timezone.

My understanding is that formatInTimeZone formats a date in the provided time zone, regardless of the system time zone, so an ISOstring (Z included) should not have a GMT offset when formatted in the UTC timezone.

I have a test for this available here: https://stackblitz.com/edit/node-rfobny?file=package.json,PPppp.test.js


 it('PPppp format', () => {
    expect(
      formatInTimeZone('2022-10-25T10:46:20.000Z', 'UTC', 'PPppp')
    ).toEqual('Oct 25, 2022, 10:46:20 AM GMT+0');

Currently getting around this problem by slicing off the GMT+1 and replacing with GMT+0, formatInTimeZone is working correctly in other formats.

MustafaHaddara commented 1 year ago

I have the same problem, here's a second reproduction case:

https://codesandbox.io/s/date-fns-playground-forked-fj2ywy?file=/src/index.js:0-23

It'll look like it works if your machine is on eastern US time (ie. America/New_York) but changing my system time to America/Los_Angeles produces the following output:

timestamp: 2023-05-26T10:30:00Z
date: Fri May 26 2023 03:30:00 GMT-0700 (Pacific Daylight Time)
zonedDate: Fri May 26 2023 06:30:00 GMT-0700 (Pacific Daylight Time)
formatted: 6:30:00 AM GMT-07:00 | Eastern Daylight Time | -0400

Notice the multiple timezones on the bottom row; I'm formatting my timestamp with the format string pppp | zzzz | xxxx so I'd expect all 3 timezones to match.

This example is using date-fns@2.29.3 and date-fns-tz@2.0.0.