top | item 46589658

Date is out, Temporal is in

464 points| alexanderameye | 1 month ago |piccalil.li

205 comments

order

tshaddox|1 month ago

This article lists several of the absurdities of the Date constructor, but only barely touches on the most unforgivable one. The example from the article is:

  // Unless, of course, you separate the year, month, and date with hyphens.
  // Then it gets the _day_ wrong.
  console.log( new Date('2026-01-02') );
  // Result: Date Thu Jan 01 2026 19:00:00 GMT-0500 (Eastern Standard Time)
In this example, the day is "wrong" because the constructor input is being interpreted as midnight UTC on January 2nd, and at that instantaneous point in time, it is 7pm on January 1st in Eastern Standard Time (which is the author's local time zone).

What's actually happening here is a comedy of errors. JavaScript is interpreting that particular string format ("YYYY-MM-DD") as an ISO 8601 date-only form. ISO 8601 specifies that if no time zone designator is provided, the time is assumed to be in local time. The ES5 spec authors intended to match ISO 8601 behavior, but somehow accidentally changed this to 'The value of an absent time zone offset is “Z”' (UTC).

Years later, they had realized their mistakes, and attempted to correct it in ES2015. And you can probably predict what happened. When browsers shipped the correct behavior, they got too many reports about websites which were relying on the previous incorrect behavior. So it got completely rolled back, sacrificed to the altar of "web compatibility."

For more info, see the "Broken Parser" section towards the bottom of this article:

https://maggiepint.com/2017/04/11/fixing-javascript-date-web...

no_wizard|1 month ago

>So it got completely rolled back, sacrificed to the altar of "web compatibility."

This is why I don't understand the lack of directives.

'use strict'; at the top of a file was ubiquitous for a long time and it worked. It didn't force rolling back incompatibilities, it let you opt into a stricter parsing of JavaScript.

It would have been nice for other wide changes like this to have like a 'strict datetime'; directive which would opt you into using this corrected behavior.

They couldn't and shouldn't do this sort of thing for all changes, but for really major changes to the platform this would be an improvement.

Or they could go all in on internal modules, like how you can import `node:fs` now. They could include corrected versions of globals like

`import Date from 'browser:date';`

has corrected behavior, for example

OptionOfT|1 month ago

I very much remember coding a function that split the string on their components and then rebuild them to ensure the date was created without time zone.

Sometimes a date is just a date. Your birthday is on a date, it doesn't shift by x hours because you moved to another state.

The old Outlook marked birthdays as all-day events, but stored the value with time-zone, meaning all birthdays of people whose birthday I stored in Belgium were now shifted as I moved to California...

teiferer|1 month ago

> sacrificed to the altar of "web compatibility."

What should they have done instead? Force everybody to detect browser versions and branch based on that, like in the olden days of IE5?

(Serious question, maybe I'm overlooking some smart trick.)

Kyro38|1 month ago

You might want to play with https://jsdate.wtf/

One can't fathom how weird JS Date can be.

sholladay|1 month ago

Personally, I like that UTC is the default time zone. Processing of dates should happen in a standardized time zone. It’s only when you want to display it that the date should become local.

netghost|1 month ago

If this is comedy, sign me up for tragedy.

This feels like something that must be the root of innumerable small and easily overlooked bugs out there.

GoblinSlayer|1 month ago

Local time is unparsable, and this case is only human readable, because humans can handle ambiguity ad hoc. Parsing it as UTC is a reasonable default for a machine parser, at least the only workable one.

cush|1 month ago

Maggie is a killer dev. Momentjs probably saved humanity millions of hours of collective coding and debugging

procaryote|1 month ago

There's a lot wrong with Javascript's Date, but the fact that it's an object is is not really in the top 10.

Would it have been nice if the Date object had been immutable? Sure, but the fact that changing the mutable object does indeed change the object shouldn't be a shock

RedShift1|1 month ago

What happened to me is I passed a date to an external library, and then after that library did its work, that date was changed. Super annoying even if you know that it's a mutable object.

chowells|1 month ago

It's definitely a shock when something else changes the date object you've been holding on to. The problem with mutable values has never been when you (that is, the local context) change them. It's always that you can't trust that nothing else (some very non-local code) does.

bilekas|1 month ago

This is a skill issue imo. Yes, if you change the referenced object you get a different value. Just because you are not paying attention to the change does not a problem of the language make.

There are million other things legitimately wrong wit JS, developers being bad at understanding referenced objects is not one of them.

LegionMammal978|1 month ago

I do find it annoying how the Temporal API, just like nearly all other datetime APIs, has 0 support for querying leap-second information in any shape or form. Suggested workarounds like temporal-tai all require plugging in a leap-second file and keeping it updated, which is especially painful for client-side JS, where you can't just download a leap-second file from someone else's site thanks to the SOP. Meanwhile, browsers update on a cadence more than sufficient to keep an up-to-date copy, but the datetime APIs refuse to expose leap-second info because they're too committed to "only UTC is in-scope for this project".

(The context is that I want to write some JS tools for astronomical calculations, but UTC conversions need leap-second info, so this trend makes it impossible to write something that Just Works™.)

schiffern|1 month ago

  >only UTC is in-scope for this project

  >tools for astronomical calculations
Pity, since UTC is objectively the wrong time for astronomical calculations. Among other problems, UTC runs slightly slower or faster depending on how far the Earth is from the Sun. UTC does not run uniformly (outside of Earth-at-sealevel), instead the length of 1 second will slightly grow or shrink depending on the current configuration of the Solar system.

As you allude to, the correct time scale for this purpose would be TBD (aka Barycentric Dynamical Time), which applies relativistic corrections to act like the atomic clock is fixed at the barycentre of the Solar system. This is the only clock that actually runs "smoothly" for the purposes of astronomical calculations.

https://stjarnhimlen.se/comp/time.html

https://www2.mps.mpg.de/homes/fraenz/systems/systems2art/nod...

nightpool|1 month ago

> where you can't just download a leap-second file from someone else's site thanks to the SOP

WDYM by this? Why does the SOP prevent a website from hosting a leap seconds file? All they need to do is set Access-Control-Allow-Origin to allow websites to access it. Or provide it as a JS file—in which case no headers are necessary at all. All the SOP prevents is you hotlinking someone else's leap-seconds file and using their bandwidth without their opt-in.

> Meanwhile, browsers update on a cadence more than sufficient to keep an up-to-date copy

Is this true? I don't know any browser right now that ships with a copy of a leapseconds data file. Adding such a data file and keeping it up to date would probably be a pretty non-trivial task for new browser developers—just for something the browser will never end up using itself. It's not like the ICU/CLDR files where browsers are going to need them anyway for rendering their own user-interface components.

burntsushi|1 month ago

> but the datetime APIs refuse to expose leap-second info because they're too committed to "only UTC is in-scope for this project".

This doesn't make sense on at least two different levels.

First, pedantically, the definition of UTC as a time scale is that it includes leap seconds. So if you're committed to UTC, then you're supporting leap seconds.

Second, and to more broadly address your point, you should say, "they're too committed to 'only the POSIX time scale is in-scope for this project.'" That more accurately captures the status quo and also intimates the problem: aside from specialty applications, basically everything is built on POSIX time, which specifically ignores the existence of leap seconds.

paulddraper|1 month ago

> I do find it annoying how the Temporal API, just like nearly all other datetime APIs, has 0 support for querying leap-second information in any shape or form.

That’s because human time keeping doesn’t use leap seconds.

mr_toad|1 month ago

Can’t you just mirror the data? You could even embed it in the javascript file itself.

hgs3|1 month ago

> like nearly all other datetime APIs, has 0 support for querying leap-second information

That's probably because you only need leap second accuracy in niche use cases, like astronomy or GPS. In JavaScript specifically, that kind of accuracy isn't needed for 99% of client-side use cases. Most date-time libraries work with POSIX time which assumes 86,400 seconds each day.

daveoc64|1 month ago

I'm really surprised at how Temporal is only just rolling out in Chrome stable.

I would have hoped it'd be ready for wider use by now.

https://caniuse.com/temporal

promiseofbeans|1 month ago

We’ve been loving using it in our Deno servers since last year. It’s been frustrating that we haven’t been able to upgrade our web client date logic yet, since even though Firefox has supported Temporal for a while, Chrome have really dragged their feet

ZeWaka|1 month ago

All the chromiums are scheduled for this week. Still TP for WebKit.

munificent|1 month ago

> When an immutable value is assigned to a variable, the JavaScript engine creates a copy of that value and stores the copy in memory

Not exactly. The language doesn't specify whether the value is copied or not and, precisely because values are immutable, there's no way for a user to tell if it was or wasn't.

For example, strings are also immutable value types, but you can be certain that no JS engine is fully copying the entire string every time you assign one to a variable or pass it to a parameter.

irjustin|1 month ago

Late to the party, I really wish everyone would copy Rails + Ruby, but specifically it's Rails additions.

2 things it got right:

1. Like the article a great API - Time.current.in_time_zone('America/Los_Angeles') + 3.days - 4.months + 1.hour

2. Rails overloads Ruby's core library Time. You're in 1 object the whole time no swap/wondering.

In the py world, pendulum is close but just like the article, it's cumbersome as it's still a separate obj (i.e. Temporal vs Date) and so you need to "figure out" what you have to manipulate or need to cast it first.

Overloading the core libs is dangerous for a whole host of reasons but for the end developer it's a pleasure to use.

If we could just do `new Date().add({ days: 1})` it would be so easier.

happytoexplain|1 month ago

>3.days - 4.months + 1.hour

Is this what it looks like? A specific concept like time units being defined as members of more general types like numbers? I.e. if I type `1.` to get auto-complete, am I going to see days, and all the rest, as options?? That API design pattern would be a nightmare!

publicdebates|1 month ago

> 3.days - 4.months + 1.hour

How is that a good thing?

> Rails overloads Ruby's core library Time. You're in 1 object the whole time regardless of what you do.

How is that a good thing?

devilsdata|1 month ago

Bit of a tangent, but I really wish JavaScript was versioned. Allowing MDN to clean up the syntax and APIs would be incredible.

tensegrist|1 month ago

typo:

    // A numeric string between 32 and 49 is assumed to be in the 2000s:
    console.log( new Date( "49" ) );
    // Result: Date Fri Jan 01 2049 00:00:00 GMT-0500 (Eastern Standard Time)

    // A numeric string between 33 and 99 is assumed to be in the 1900s:
    console.log( new Date( "99" ) );
    // Result: Date Fri Jan 01 1999 00:00:00 GMT-0500 (Eastern Standard Time)
the second interval should start at 50, not 33

rjrjrjrj|1 month ago

Good article, but “Java deprecated their Date way back in 1997” is not exactly true. They deprecated a lot of methods and constructors in JDK1.1 when Calendar was introduced, but the class itself was never deprecated and it was the preferred way to represent a point in time until the “modern” approach was provided in java.time in JDK8 (c2014)

shellac|1 month ago

Not exactly true, but they deprecated absolutely everything that made it a date. It expresses deep regret in the medium of annotations:

https://javaalmanac.io/jdk/1.2/api/java/util/Date.html

(I can't find the 1.1 docs, but they were the same)

It's one of my favourite examples of how languages pretty much always get date and time hopelessly wrong initially. Java now has one of the best temporal APIs.

jackfranklyn|1 month ago

The timezone handling alone makes Temporal worth the wait. I've lost count of how many bugs I've shipped because Date silently converts everything to local time when you least expect it.

The ZonedDateTime type is the real win here - finally a way to say "this is 3pm in New York" and have it stay 3pm in New York when you serialize and deserialize it. With Date you'd have to store the timezone separately and reconstruct it yourself, which everyone gets wrong eventually.

Only downside I can see is the learning curve. Date was bad but it was consistently bad in ways we all memorized. Temporal is better but also much larger - lots of types to choose between.

GoblinSlayer|1 month ago

>I've lost count of how many bugs I've shipped because Date silently converts everything to local time when you least expect it.

You mean methods like getHours/getUTCHours?

xeckr|1 month ago

I suspect that the arrival of ChatGPT caused traffic to the MDN Date API documentation to go down by at least half.

qsort|1 month ago

To be fair it's exactly the type of stuff I'd be glad if I never have to think about again.

ivanjermakov|1 month ago

Go down by half of user traffic, go up 10x of crawler traffic.

thoughtpalette|1 month ago

Built a scheduler with pretty much all my moment/moment-tz questions answered through ChatGPT. One of the things it excels at, crawling long lived API documentation, answers, etc.

themafia|1 month ago

The Date API is fine and relatively normalized. Once you understand it it's very easy to work with. It's biggest problem is that it simply does not support timezones, which is the main reason to use Temporal.

Aardwolf|1 month ago

Checking its API, I'm surprised that Temporal.Duration has a constructor with many parameters for years, months, days, ... all the way to nanoseconds, while Temporal.Instant has no way at all to create it given a current year/month/day, only from unix timestamp equivalents (or strings)

That seems to be functionality you'd want to have? Or is the intention you convert your numbers to string first and then back to a Temporal.Instant?

tshaddox|1 month ago

It's perfectly reasonable to default seconds, minutes, hours, etc. to zero in the Duration constructor. But for Instant, it doesn't make sense to default those to zero unless you specify time zone offset.

And indeed, the static method Instant.from does accept an RFC 9557 string, which requires a 2-digit hour and a time zone offset, but can omit minutes and seconds:

Temporal.Instant.from("2026-01-12T00+08:00")

Macha|1 month ago

I guess they don’t want people getting confused between local and UTC values for the fields in the constructor (especially as if it took local values it would need to handle DST transitions)

sheept|1 month ago

it's because a year/month/date isn't enough to uniquely identify an instant in time. you can create a Temporal.PlainDate(Time) with those values though, then convert to the other types depending on what you need and it needs (e.g. time zone)

ckocagil|1 month ago

Late by a decade or more (JSR310 was released in 2014), but still a good development. I've tried convincing colleagues to use js-joda in the past but they thought they were keeping it simple by sticking to moment.js. They weren't.

atoav|1 month ago

If you ever find yourself in the situation where you have to decide whether 99 parses to year 1999 and what to do with 100 and where to draw the line between parsing the 90s and current years, you should probably pause, reath in andaout and take a long walk instead. And after you return you should scrap the whole thing and go back to the drawing board.

Implementing such a feature has not only no value, it has negative value. When you program libraries or interfaces you ahould think aboit how people will use it 95% of the time and mane that usecase as simple, predictable and free of potential footguns as possible. This is the opposite of that. It feels like something 15 year old me would have programmed after reading the first book on PHP and not something anybosy with any experience could have thought to be a good thing.

Someone1234|1 month ago

Except Temporal is not, in fact, out.

https://caniuse.com/temporal

The current global availability of native Temporal is 1.81%. For context, IE11(!) has a higher global usage than Temporal has native support. For my organization, this likely means we're years from being able to use Temporal in production, because getting the polyfills approved is such a hassle.

Keep in mind that even as of December last year, Chrome didn't ship with it yet (i.e. support is less than one month old). Safari still does not.

senfiaj|1 month ago

Chrome will ship in 144. It's about to release.

themafia|1 month ago

Thankfully temporal-polyfill only depends on temporal-spec. It was pretty easy to get that through.

schiffern|1 month ago

  >It wholesale does not understand the concept of daylight savings time
While we're nitpicking (which I wholly support, by the way) it's "daylight saving time."

Cheers, great read.

austin-cheney|1 month ago

The article is super weird. It never mentions Date.now(). It dances around the subject and exhaustively mentions the equivalent convention for Temporal.

If you want Date to act like Temporal then only use Date.now() as your starting point. It generates the number of milliseconds since 1 Jan 1970. That means the starting output is a number type in integer form. It does not represent a static value, but rather the distance between now and some universal point in the past, a relationship. Yes, Temporal is a more friendly API, but the primary design goal is to represent time as a relational factor.

Then you can format the Date.now() number it into whatever other format you want.

ffsm8|1 month ago

the article has examples of unexpected behavior with timestamps too, so... How do you covert to your desired format without going through Date? Please don't say date-fns

whiterook6|1 month ago

So, hold on--the author's soul-breaking complaint isn't all of the "quirks" and inconsistencies with the Date functions, but rather the fact that it's an object? Specifically, an object with mutable properties in a language when all objects have mutable properties?

I mean, the author's conclusion is correct. But I disagree with the rationale. It's like hating an evil dictatorship because they use the wrong font in their propaganda.

Yaggo|1 month ago

The old JS Date API is far from perfect and I'm happy it being replaced, but part of the problem is various string-based formats and people being sloppy using them. Not to mention general complexity in time/date concept with timezones, summer time, leap seconds, etc.

For string format, just stick with ISO 8601. If you need to parse less-standard formats, use a robust library of your choise. The standard library should not try to support parsing zillion obscure formats. Outputting localized / human-readable format should be a responsibility of localization API anyway.

I also think that many libraries/APIs involving formatting things have some US centric design limitations, i.e. tendency to treat US formats as native and international support is often a bit after-thought. Especially with older stuff like the JS Date API.

ReptileMan|1 month ago

The problem with the date format is that the US one absolutely totally insane. Whenever you use something ordered you have to choose ordering. For date US choose the absurd kind. Y-d-m should never have been used. Remove that and around 90% of the string based format problems disappear.

mastermedo|1 month ago

I dislike the "Plain" prefix to the temporal objects, e.g. PlainDateTime. The prefix says nothing about the behavior of the class. Plain as opposed to what; ZonedDateTime. I would prefer "Local" over "Plain"; LocalDateTime.

exyi|1 month ago

Local would imply the date is in the current machine timezone, while PlainDateTime is zoneless. It may be in the server timezone, or anything else. The main difference is that it does not make sense to convert it to Instant or ZonedDateTime without specifying the timezone or offset

agos|1 month ago

you have to consider the existence of PlainDate which is a Date without both a Time and a TimeZone

LoganDark|1 month ago

Is Temporal even in though? Last I checked (last year or so), I had to use some bleeding edge version of Firefox to get it, and absolutely nothing else had it. I do agree though it's lovely, and I'd love to see native support for it.

noelwelsh|1 month ago

Nope. Only Firefox and Chrome have it, in their latest versions. No Safari or Edge support yet. So this article is a bit premature (unless you use the polyfill.)

sublinear|1 month ago

> My complaint is about more than parsing or syntax or “developer ergonomics” ... My problem with Date is that using it means deviating from the fundamental nature of time itself.

I don't really have a problem with the substance of this blog post. I have a problem with this exaggerated writing style. It means deviating from the fundamental purpose of writing itself!

I had to scroll all the way to the end to find the actual point, and it was underwhelming.

> Unlike Date, the methods we use to interact with a Temporal object result in new Temporal objects, rather than requiring us to use them in the context of a new instance

Bro, just be honest. This entire blog post was totally about developer ergonomics and that's okay. We all hate the way Date works in javascript.

happytoexplain|1 month ago

It's not an exaggeration - you're used to dramatic phrases that use similar wording ("fundamental nature of time itself"), but in this case it's a regular old literally-true statement. Date is used to represent two things: Timestamps and human times (date, time, tz). But it only actually represents the former. Using it to represent the latter is a hack we simply put up with.

sfink|1 month ago

Pedantically, Temporal also deviates from the fundamental nature of time itself. Temporal.Instant? In which accelerating frame of reference? It supports equality, which is a nonsense concept.

lightningspirit|1 month ago

If you do `new Date('2025-11-31T00:00:00Z')` you get `2025-12-01T00:00:00.000Z`, which is weird and potential cause for bugs when parsing date string from input. Right because of these inconsistency I created a small package backed as Regex to validate Date string input before throwing it on `new Date(stringDate)`.

https://www.npmjs.com/package/iso-8601-regex

krzkaczor|1 month ago

I hope that we can move to using DurationLike objects in place of ms (or sometimes seconds?) as plain numbers like in `setInterval`. `sleep({seconds 5})` is so much better than `sleep(5000)`.

recdnd|1 month ago

Temporal feels less like a replacement for Date and more like an admission that Date was trying to represent too many different concepts at once.

moralestapia|1 month ago

Not yet, adoption is kind of poor atm.

This is not to detract from the quality of the article which is a top-notch introduction to this new API.

nubskr|1 month ago

Temporal being immutable alone is worth whatever migration pain we're about to endure.

pmarreck|1 month ago

I actually had no idea javascript Date was this bad until now.

acdha|1 month ago

What’s really sad is that this was obvious in 1995. If they’d taken a slightly longer time back then, millions of developers would have avoided tripping over those unnecessary quirks.

noduerme|1 month ago

I use Moment.

And a few years later, I'm still using Moment.

And now I'm still using Moment.

And now...

nchmy|1 month ago

Anyone have links that indicate when Safari might have it?

promiseofbeans|1 month ago

The JSC people have been relatively involved as the spec was developed, and they have it behind a flag in their TP builds . It’ll probably ship in Safari once it ships in Chrome so that their users don’t complain about broken websites

mastermage|1 month ago

At one point we realy need to make web2. With a blank slate and maybe a much more sane implementation of Websites. Maybe just allow CSS Html and all functions need to run over Wasm. Wasm would need to get some more features for that but at least, you would avoid allot of bullshit.

zvqcMMV6Zcr|1 month ago

I am not sure if I like mixing value and formatting in single object. On other hand anything will be an improvement compared to that terrible old API.

happytoexplain|1 month ago

Temporal objects do not store formatting information. Unless you mean e.g. dropping the time, using a different time zone, etc - but those aren't formatting changes, they logically change the semantics of the data. Just like `myInt += 1` is not changing the "formatting" of `myInt`.

Remember: Date and Temporal objects are logically different things. A Date represents an absolute point in time (a timestamp), while a Temporal object represents a human time (calendar date, clock time, time zone). The fact that Dates are used to represent human time for lack of a better structure is the entire problem statement - the hole that all these other APIs like Temporal try to fill in.

jdonaldson|1 month ago

[deleted]

DarkNova6|1 month ago

What’s the superior alternative and in which domains?

TZubiri|1 month ago

Yes, you should use shiny new library instead of the glitchy javascript standard..

Unless you want your website to run in browsers older than a year

Maybe in 10 years we can start using the shiny new thing?

wesselbindt|1 month ago

Polyfills are a thing, no?