Description
Add Date.fromTemporalInstant
and Date.fromTemporalZonedDateTime
function to easily create Dates from Temporal values.
Advantages:
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?) For adoption, it's important that converting to/from Dates is easy and rebost.
This proposal offers convenience functions:
Date.fromTemporalInstant(instant)
Date.fromTemporalZonedDateTime(zonedDateTime)
Concerns:
See:
- Better conversion between legacy Date and Temporal tc39/proposal-temporal#515
- Shorter code to create an Absolute, especially from a legacy Date tc39/proposal-temporal#751
- Convert Instant/ZonedDateTime -> Date tc39/proposal-temporal#2769 (comment)
Converting from ZonedDateTime and Date suggests that Date has time zone (not just offset)
Downcasting is common. No one would think that ZonedDateTIme -> Instant suggests that Instant has time zone.
Usage of Date should be discouraged
This sentiment vastly underestimates the stickiness of current Date usage and the necessity of easy conversion between the two.
new Date(instant.epochMilliseconds) is easy enough
With all due respect, not at all. For example MDN list the following constructor calls for Date:
new Date()
new Date(value)
new Date(dateString)
new Date(dateObject)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
I'm supposed to know that I should pass one arg, and that arg is milliseconds? Come on, man.
Prior art:
N/A
Constraints / corner cases:
N/A