marnusw / date-fns-tz

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

Unexpected behavior zonedTimeToUtc #243

Open willwillems opened 12 months ago

willwillems commented 12 months ago

I am in UTC + 3. I am running the following code:

zonedTimeToUtc(new Date(), Intl.DateTimeFormat().resolvedOptions().timeZone)

Where the second argument is correctly providing my timezone.

What am I expecting here: "Given a date and any time zone, returns a Date with the equivalent UTC time." I am expecting to see a Date object with a value that represents my current time (for example "14:47") in UTC, I would effectively see an offset of 3 hours as the result. Instead I'm getting back: the exact same timestamp.

However. When I run:

zonedTimeToUtc(new Date())

The result is what I would expect above.

The fact that this function works without a second argument is not documented in the readme or anywhere else I could find.

mikomonday commented 8 months ago

Similar to your initial code I had been extracting the timezone of the date fed into it thinking why it was necessary in the first place.

Ironically the function is typed to require this second value as well, I'm passing null as a second parameter just so TS isn't complaining which really doesn't make sense.