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

TZDate constructor time zone appears to have no effect with a date string #22

Open
ianawilson opened this issue Oct 16, 2024 · 1 comment

Comments

@ianawilson
Copy link

When I use a date string in the TZDate constructor, it appears that the time zone has no effect.

My machine is in Pacific Daylight Time and all of these produce the same time, midnight UTC interpreted in my machine's local time:

new TZDate("2024-10-31", "America/Los_Angeles")
Wed Oct 30 2024 17:00:00 GMT-0700 (Pacific Daylight Time)
new TZDate("2024-10-31", "America/Denver")
Wed Oct 30 2024 17:00:00 GMT-0700 (Pacific Daylight Time)
new TZDate("2024-10-31", "UTC")
Wed Oct 30 2024 17:00:00 GMT-0700 (Pacific Daylight Time)
new TZDate("2024-10-31", "Asia/Singapore")
Wed Oct 30 2024 17:00:00 GMT-0700 (Pacific Daylight Time)

I see that it is possible to use parseISO() to accomplish this, eg:

date_fns.parseISO("2024-10-31", {in: tz("America/Los_Angeles")})
Thu Oct 31 2024 00:00:00 GMT-0700 (Pacific Daylight Time)
date_fns.parseISO("2024-10-31", {in: tz("America/Denver")})
Wed Oct 30 2024 23:00:00 GMT-0700 (Pacific Daylight Time)
date_fns.parseISO("2024-10-31", {in: tz("UTC")})
Wed Oct 30 2024 17:00:00 GMT-0700 (Pacific Daylight Time)
date_fns.parseISO("2024-10-31", {in: tz("Asia/Singapore")})
Wed Oct 30 2024 09:00:00 GMT-0700 (Pacific Daylight Time)

If the current behavior is expected, can you clarify what the purpose of using TZDate() with a date string and a time zone in the documentation? And what's expected?

@johnny-T
Copy link

johnny-T commented Oct 16, 2024

The way I understand it is that if you instantiate TZDate using string, the underlying date is always the same. So if you have 2024-10-31, which is interpreted as UTC in Date object, the underlying date will always be 2024-10-31T00:00:00Z, which would be displayed as Wed Oct 30 2024 17:00:00 GMT-0700 (Pacific Daylight Time) in console in your case.

But the difference between using Date and TZDate is that when calling it's methods, you will get results for the target timezone, so calling new TZDate("2024-10-31", "Asia/Singapore").toString() would return Thu Oct 31 2024 08:00:00 GMT+0800 (Singapore Standard Time), which is the GMT+8 equivalent of 2024-10-31 in UTC.

You can perhaps take a look at discussion for issue #9, where creating TZDate instances is discussed. It is possible to use transpose function if you want to take a certain date and shift it so you get the same date but in different timezone. Or you can use parseISO as you mentioned.

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