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

How do I create a date in a specific timezone? #36

Open
chalupagrande opened this issue Jan 8, 2025 · 4 comments
Open

How do I create a date in a specific timezone? #36

chalupagrande opened this issue Jan 8, 2025 · 4 comments

Comments

@chalupagrande
Copy link

I have a date like: 2025-01-08T18:00

How do I tell date-fns that this is in America/Chicago timezone?

@jawnothin
Copy link

I came here to get the answer to that specific question... because right now it just assumes the timezone on the local device, which may or may not be correct.

@cbejensen
Copy link

cbejensen commented Jan 9, 2025

Seems like TZDate reads ISO-like strings as UTC or local, while passing in the elements as individual args achieves the desired result:

console.log(new TZDate(2025, 0, 8, 18, 'America/Chicago').toString());
// Wed Jan 08 2025 18:00:00 GMT-0600 (Central Standard Time)

@simoneb
Copy link

simoneb commented Jan 29, 2025

I've been looking into the same issue, which is somewhat unexpected because it suggests that the TZDate constructor applies different logic based on whether a date string is passed, or the individual bits (year, month, day, ...).

In the string case, it is "assumed" to be in a certain timezone, then convered into the desired timezone.
In the other case, it is interpreted as being that specific point in time in the desired timezone.

Is there any way to achieve the second behavior using the string format as an input?

@zilet
Copy link

zilet commented Jan 31, 2025

Not sure why there is a different behavior between string and numerical values. I expect that TZDate will create a Date in the passed timezone from string that does not contain zone (and not append system timezone and convert it).

date-fns-tz does this as expected.

e.g.

fromZonedTime(timeframeDurationsIso("2025-01-31T16:00" "America/New_York);

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

5 participants