logoalt Hacker News

Temporal: A nine-year journey to fix time in JavaScript

317 pointsby robpalmertoday at 3:35 PM119 commentsview on HN

Comments

julius_eth_devtoday at 7:06 PM

Nine years is a long time, but honestly it tracks with how deeply broken Date has been since Brendan Eich cargo-culted java.util.Date in 1995. The real win with Temporal isn't just immutability or timezone support — it's that PlainDate and ZonedDateTime finally give us types that match how humans actually think about time. I've lost count of how many bugs I've shipped because Date silently coerces everything to UTC instants when half the time what you actually have is a "wall clock" value with no timezone attached.

VanCodingtoday at 4:56 PM

A big step in the right direction, but I still don't like the API, here's why: Especially in JavaScript where I often share a lot of code between the client and the server and therefore also transfer data between them, I like to strictly separate data from logic. What i mean by this is that all my data is plain JSON and no class instances or objects that have function properties, so that I can serialize/deserialize it easily.

This is not the case for Temporal objects. Also, the temporal objects have functions on them, which, granted, makes it convenient to use, but a pain to pass it over the wire.

I'd clearly prefer a set of pure functions, into which I can pass data-only temporal objects, quite a bit like date-fns did it.

show 6 replies
nekevsstoday at 4:11 PM

Super happy to see Temporal accepted!

Congrats to all the champions who worked super hard on this for so long! It's been fun working on temporal_rs for the last couple years :)

the__alchemisttoday at 7:05 PM

Maybe I will be able to move away from my custom/minimal DT lib, and ISO-8601 timestamp strings in UTC. JS datetime handling in both Date and Moment are disasters. Rust's Chrono is great. Python's builtin has things I don't like, but is useable. Date and Moment are traps. One of their biggest mistakes is not having dedicated Date and Time types; the accepted reason is "Dates and times don't exist on their own", which is bizarre. So, it's canon to use a datetime (e.g. JS "Date") with 00:00 time, which leads to subtle errors.

From the link, we can see Temporal does have separate Date/Time/Datetime types. ("PlainDate" etc)

plucastoday at 4:13 PM

Would have been interesting to connect back to Java's own journey to improve its time APIs, with Joda-Time leading into JSR 310, released with Java 8 in 2014. Immutable representations, instants, proper timezone support etc.

Given that the article refers to the "radical proposal" to bring these features to JavaScript came in 2018, surely Java's own solutions had some influence?

show 2 replies
bnbtoday at 3:46 PM

Can't wait for it to land in the server-side runtimes, really the last thing preventing me from adopting it wholesale.

show 3 replies
zvqcMMV6Zcrtoday at 4:20 PM

> Safari (Partial Support in Technology Preview)

Safari confirmed as IE Spiritual successor in 2020+.

show 2 replies
johncomposedtoday at 6:33 PM

As a side note, huge fan of Promise.allSettled. When that dropped it cleaned up so much of the code I was writing at the time.

wpollocktoday at 5:33 PM

> "It was a straight port by Ken Smith (the only code in "Mocha" I didn't write) of Java's Date code from Java to C."

This is funny to me; Java's util.Date was almost certainly a port of C's time.h API!

xp84today at 6:25 PM

They travelled through time (forward, at 1X) by nine years to do this for us. I appreciate it.

tracker1today at 6:49 PM

Looking at the caniuse results... f*king Safari (and Opera)...

https://caniuse.com/temporal

show 2 replies
kemayotoday at 5:31 PM

> Developers would often write helper functions that accidently mutated the original Date object in place when they intended to return a new one

It's weird that they picked example code that is extremely non-accidentally doing this.

SoftTalkertoday at 5:52 PM

It's been a while since I worked in JS but dealing with dates/times, and the lack of real integer types were always two things that frustrated me.

philipallstartoday at 4:53 PM

> have to agree on what "now" means, even when governments change DST rules with very little notice.

I didn't spot how Temporal fixes this. What happens when "now" changes? Does the library get updated and pushed out rapidly via browsers?

show 2 replies
redbelltoday at 4:36 PM

Oh, for a second, TeMPOraL (https://news.ycombinator.com/user?id=TeMPOraL) came to my mind!

ventuss_ovotoday at 7:10 PM

interesting point about immutability

samwhotoday at 4:04 PM

Thanks for linking to my silly little quiz in the article! :)

hungryhobbittoday at 5:12 PM

From the article:

    const now = new Date();
The Temporal equivalent is:

    const now = Temporal.Now.zonedDateTimeISO();
Dear god, that's so much uglier!

I mean, I guess it's two steps forward and one step back ... but couldn't they have come up with something that was just two steps forward, and none back ... instead of making us write this nightmare all over the place?

Why not?

    const now = DateTime();
show 1 reply
sharktheonetoday at 4:08 PM

Very happy for it finally being there!

darepublictoday at 5:20 PM

My playbook for JavaScript dates is.. store in UTC.. exchange only in UTC.. convert to locale date time only in the presentation logic. This has worked well for me enough that Im skeptical of needing anything else

show 4 replies
normie3000today at 4:08 PM

No mention of JodaTime?

NooneAtAll3today at 5:34 PM

so Temporal is copying cpp's std::chrono?

show 1 reply
jon_kupermantoday at 3:43 PM

What a journey!

ChrisArchitecttoday at 4:41 PM

A good article and discussion from January:

Date is out, Temporal is in

https://news.ycombinator.com/item?id=46589658

virgil_disgr4cetoday at 4:07 PM

Pretty big fan of Temporal. Been using the polyfill for a while. Very nice to use a modern, extremely well thought-through API!

ChrisArchitecttoday at 4:42 PM

Aside: Bloomberg JS blog? ok.

show 3 replies
patchnulltoday at 4:06 PM

The most underappreciated design decision in Temporal is making everything immutable. Half the Date bugs I have debugged over the years come from someone calling setMonth() or setHours() on a Date object that was shared across call sites. The other half come from the implicit local timezone conversion in the Date constructor. Temporal fixes both by forcing you to be explicit about timezones through ZonedDateTime vs PlainDate, and by returning new objects instead of mutating. Nine years is a long time, but given how many TC39 proposals ship half-baked and then need follow-up proposals to fix the gaps, I would rather they got this one right.

show 8 replies
patchnulltoday at 7:23 PM

[flagged]

show 1 reply
newzinotoday at 5:24 PM

[dead]

aplomb1026today at 5:31 PM

[dead]

ilovesamaltmantoday at 6:30 PM

this is reallly fucking interesting

mind if I write an article about this on ijustvibecodedthis.com ?

patchnulltoday at 7:06 PM

[flagged]