8000 Convert Instant/ZonedDateTime -> Date · Issue #31 · js-temporal/proposal-temporal-v2 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Convert Instant/ZonedDateTime -> Date #31
Open
@pauldraper

Description

@pauldraper

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0