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

Convert Instant/ZonedDateTime -> Date #2769

Closed
pauldraper opened this issue Feb 12, 2024 · 2 comments
Closed

Convert Instant/ZonedDateTime -> Date #2769

pauldraper opened this issue Feb 12, 2024 · 2 comments

Comments

@pauldraper
Copy link

pauldraper commented Feb 12, 2024

Converting to Date currently requires:

new Date(instant.epochMilliseconds)
new Date(zonedDateTime.epochMilliseconds)

This is awkward, and requires remembering the unit for Dates. (seconds? millis? nanos?)

I suggest offering convenience functions:

Date.fromTemporalInstant(instant)
Date.fromTemporalZonedDateTime(zonedDateTime)

For adoption, it's important that converting to/from Dates is easy and rebost.

@ptomato
Copy link
Collaborator

ptomato commented Feb 13, 2024

Thanks for taking the time to give feedback!

There's more background on this decision in #515 and #751 if you'd like more reading material.

Direct conversion between legacy Date and ZonedDateTime is actually something that we intentionally omitted. The data model of Date does not include a time zone, although many developers incorrectly believe that it does because it has two methods for each unit such as getHour()/getUTCHour() that use the current system time zone and UTC, respectively. We felt that converting between ZonedDateTime and Date was likely to lead to confusion in that regard. Additionally, it would have introduced a new point where system information (the user's time zone setting) was accessed implicitly: something that TC39 would not accept.

Although I can't find it documented anywhere now, if my memory serves, we considered that new Date(instant.epochMilliseconds) was sufficiently convenient for a conversion whose usage would drop off after an initial period of adoption, and didn't need a separate method.

At this stage of the proposal, the scope is nailed down and we aren't going to add new methods. If you'd like, you can open an issue at js-temporal/proposal-temporal-v2 and we can track whether we get a lot of requests for it.

@pauldraper
Copy link
Author

Okay, I filed an issue for v2: js-temporal/proposal-temporal-v2#31

Though I'll go on record that I'm skeptical on adoption without having an easy(ier) Temporal <-> Date conversion.

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