Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating from date-fns-tz #33

Open
kkwasnyrtb opened this issue Dec 17, 2024 · 1 comment
Open

Migrating from date-fns-tz #33

kkwasnyrtb opened this issue Dec 17, 2024 · 1 comment

Comments

@kkwasnyrtb
Copy link

kkwasnyrtb commented Dec 17, 2024

Hi, I'm trying to migrate from date-fns-tz. According to the documentation, transpose is the date-fns-tz's toZonedTime equivalent. However, it seems both functions return different results depending on the time zone provided.

Please take a look at the code below.

const date = new Date("2024-12-17T12:00:00.000Z");

const timezone1 = "Europe/Warsaw";
const zoned1 = toZonedTime(date, timezone1);
const transposed1 = transpose(date, tz(timezone1));

const timezone2 = "Europe/Kyiv";
const zoned2 = toZonedTime(date, timezone2);
const transposed2 = transpose(date, tz(timezone2));

const timezone3 = "Pacific/Kiritimati";
const zoned3 = toZonedTime(date, timezone3);
const transposed3 = transpose(date, tz(timezone3));

console.log(zoned1);
console.log(zoned2);
console.log(zoned3);
console.log("");
console.log(transposed1);
console.log(transposed2);
console.log(transposed3);

The output clearly shows that the results of toZonedTime (the first three lines) and transpose (the last three lines) differ.

Tue Dec 17 2024 13:00:00 GMT+0100 (Central European Standard Time)
Tue Dec 17 2024 14:00:00 GMT+0100 (Central European Standard Time)
Wed Dec 18 2024 02:00:00 GMT+0100 (Central European Standard Time)

Tue Dec 17 2024 13:00:00 GMT+0100 (Central European Standard Time)
Tue Dec 17 2024 12:00:00 GMT+0100 (Central European Standard Time)
Tue Dec 17 2024 00:00:00 GMT+0100 (Central European Standard Time)

Does it mean toZonedTime cannot be simply changed to transpose and some additional work is needed when migrating from date-fns-tz? Or maybe I'm doing something wrong?

@kkwasnyrtb kkwasnyrtb changed the title Unexpected transpose results Migrating from date-fns-tz Dec 17, 2024
@dvlden
Copy link

dvlden commented Dec 19, 2024

I was experiencing the same problem. I never heard about transpose, but I thought that I need to do something like this:

// making new TZDate from system time, casting `toISOString` because otherwise it's same time as with `new Date()`

parseISO(new TZDate(new Date(), 'America/Chicago').toISOString(), {
  in: tz('America/Chicago'),
})

Output is still incorrect

So now that you mentioned transpose, I achieve the same result as toZonedTime using following logic:

transpose(new TZDate(new Date(), 'America/Chicago'), TZDate)

I am unsure if this solution is the correct solution, but it works for me. I'll keep an eye on this issue, to learn more when someone with more experience with this library hops in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants