marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected behavior zonedTimeToUtc

willwillems opened this issue · comments

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.

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.