Closed alanhussey closed 4 years ago
I think the recommended idiom to convert from Date to Temporal.DateTime is basically Temporal.DateTime.from({miliseconds: +date})
. You can convert in the other direction with new Date(temporalDateTime.getEpochMiliseconds())
.
I don't think we should provide transparent interoperability since the logical domains are just different--Temporal is strict on purpose, to avoid logic errors. I wouldn't be opposed to adding conversion functions, but I'm also not sure they're necessary.
EDIT: One of the above conversion functions was incorrect; see https://github.com/tc39/proposal-temporal/issues/299#issuecomment-565714020
I don't think we should provide transparent interoperability […] I wouldn't be opposed to adding conversion functions, but I'm also not sure they're necessary.
Agreed, on both counts. I think your examples are relatively clear and terse (although it wasn't my first thought to use milliseconds-since-the-epoch as the interchange value). If Temporal were a library, I'd suggest that those examples be documented somewhere.
What @littledan recommended is actually exactly wrong and would thrown an error.
Temporal.DateTime.from({milliseconds}) will throw for milliseconds > 999
That’s what Temporal.Absolute is for!
Temporal.Absolute.fromEpochMilliseconds(dateObj) will translate a Date object to the Temporal world.
new Date(absolute.getEpochMilliseconds()) goes the other way.
To be honest, Temporal.Absolute.fromEpochMilliseconds()
is a little bit lengthy and not easy to remember, could we have a better name?
@hax please feel free to make a suggestion. Criteria are:
@hax do you have any alternatives in mind? The method itself is just named fromEpochMilliseconds
which seems rather fair, given that it constructs an Absolute
object from epoch ms.
I don't have concrete name alternatives for fromEpochMilliseconds
now, but maybe we can make Temporal.Absolute.from(legacyDate: Date)
work for this issue?
There is also another small issue about name (maybe need a separate issue), the word Epoch
should be UnixEpoch
for accuracy, because there are many different epochs.
To be honest,
Temporal.Absolute.fromEpochMilliseconds()
is a little bit lengthy and not easy to remember, could we have a better name?
I disagree, the method name is very clear on what it does and what it expects, it doesn’t seem too long to me. If I saw “from” I would need to look up (mdn?) what that accepts which outweighs the cost of a longer name.
@alanhussey please see my interop examples for working with JS Dates https://github.com/tc39/proposal-temporal/pull/400
We have cookbook examples showing how to do this now, so I think this can be closed.
Hi, I've been lurking for a bit, so I don't think this has been discussed yet (and my search attempts haven't turned anything up).
What (if any) is the story around interoperability with existing
Date
? I understand that in a greenfield application it doesn't make a lot of sense to mix usage of Temporal and "legacy" Dates, but I can see a lot of scenarios where being able to translate back and forth would be valuable.A couple examples:
Temporal.DateTime
objects. This webapp already has a lot of code that works exclusively with plain Dates. It'd be nice if I could write a small wrapper around this datetime-picker that returns Dates instead.Both of these seem solvable in userspace, but I anticipate that there are some tricky edge cases that I, a normal software developer, might not handle correctly. Are there any plans to provide convenient methods to translate Date↔Temporal?