Remix.run Logo
tshaddox 11 hours 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 8 hours ago | parent | next [-]

>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

WorldMaker 3 hours ago | parent | next [-]

To be fair, the new opt-in "use strict" here is "switch to Temporal". It's a new, stricter namespace object. Old Date code gets the old Date code quirks, new code gets the nice new Temporal API.

Internal modules would be handy in theory to maybe keep from having to dig through a thesaurus every time browsers decide to add a new, stricter version of an older API. Internal modules have even been proposed to TC-39 as a recommended way to continue to expand the JS API. Last I checked on that proposal it was stuck behind several concerns including:

1. Feature detection: detecting if Temporal available is as easy as `if ('Temporal' in globalThis) {}`, but detecting if a module import is missing is a bit harder. Right now the standard is that loading a module fails with an Error if one of its imports fails. You can work around that by doing a dynamic import inside a try/catch, but that's a lot of extra boilerplate compared to `const thingINeed = 'someApi' in globalThis ? someApi() : someApiPolyfill()`. I've seen multiple proposals on that front from extensions to import maps and `with { }` options on the import itself.

2. Bikeshedding (and lots of it): defining a URI scheme like `browser:` or `standard:` takes a bunch of thought on how you expand it. If it is just `browser:some-api` you run the risk of eventually polluting all the easy names in the exact way people worry about the risk of over-polluting `globalThis` (and in the way that it can be weirdly hard to find an available one-word name on npm), you've just moved the naming problem from one place to the other. On the other side, if you go down the road of something like `es-standard:https://tc39.es/ecma262/2025/v1/final-draft/Temporal`, even (especially) assuming users would mostly importmap that to something shorter you've recreated XMLNS URIs in a funny new hat and people who use JS all certainly have plenty of opinions on XMLNS URIs, many are very vocal in their hatred of it, but also they came out of a strong backwards incompatibility fixing desire exactly like this. (As they say time is a flat circle.)

echelon 41 minutes ago | parent [-]

> To be fair, the new opt-in "use strict" here is "switch to Temporal".

This. Don't break old code, just provide new best practices.

Update linters (or ideally first class language rules, like in Rust's "edition"s), to gradually kill off old behavior. Without having to do a decade long Python 2 -> 3 migration.

Temporal is nice. It learned from the many failures and dead bodies that came before it. And it had lots of good implementations to look at: Joda Time, Chrono, etc.

josephg 2 hours ago | parent | prev | next [-]

> 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.

That would be ugly, because you'd want some parts of your program (eg libraries) to use the old behaviour, and other parts might want the new behaviour. How would you minify multiple modules together if they all expect different behaviour from the standard library?

In my opinion the right way to do this is to have multiple constructors (as Obj-C, swift, C and rust all do). Eg:

    let d = new Date(...) // old behaviour (not recommended for new code)
    
    let d = Date.fromISOString(...) // fixed behaviour
The big downside of this is that its tricky to keep track of which fields and functions people should really stop using in modern javascript. It'd be nice if there was a way to enable more shouty warnings during development for deprecated JS features.
hnlmorg 5 hours ago | parent | prev | next [-]

This was the approach Perl took and much as I love(d) that language, it do get pretty out of hand after a while if you wanted to adopt any newer or stricter language features.

AceJohnny2 3 hours ago | parent [-]

> it do get pretty out of hand after a while if you wanted to adopt any newer or stricter language features.

How does it get out of hand?

FWIW, I just do `use v5.32;` or similar to opt-in to everything from that version.

https://perldoc.perl.org/functions/use#use-VERSION

Of course, if you instead want to pick-and-choose features, then I can see the list growing large.

thayne 3 hours ago | parent | prev | next [-]

Maybe something like rust's editions, where you can opt into a set of breaking changes made at a certain time.

3 hours ago | parent | prev [-]
[deleted]
OptionOfT 10 hours ago | parent | prev | next [-]

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...

abustamam 9 hours ago | parent | next [-]

I always found it weird when systems code dates as DateTime strings. There needs to be a different primitive for Date, which is inherently timezone-less, and DateTime, which does require a timezone.

After having a bunch of problems with dealing with Dates coded as DateTime, I've begun coding dates as a Date primitive, and wrote functions for calculation between dates ensuring that timezone never creeps its way into it. If there is ever a DateTime string in a Date column in the database, it's impossible to know what the date was supposed to be unless you know you normalized it at some point on the way up.

Then I found that a lot of DatePicker libraries, despite being in "DATE" picker mode, will still append a local timezone to its value. So I had to write a sanitizer for stripping out the TZ before sending up to the server.

That said, I am pretty excited about Temporal, it'll still make other things easier.

cpmsmith 7 hours ago | parent | next [-]

Temporal does have PlainDate, which is the Date primitive you're describing (by a different name, presumably to not collide with the old Date type).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

abustamam an hour ago | parent [-]

This is great! Thanks for sharing

mjevans 2 hours ago | parent | prev [-]

There needs to be a difference between an Instant, an Instant at an Observed Location, and a 'Specification for constructing a date that might or might not have already passed or pass in the future'.

E.G. in a conversation "Lets open the shop at 9am every day that it isn't closed." Is a fairly simple recurrence, with some exceptions*. If timezones change the scheduled time remains evaluated again on each day.

abustamam an hour ago | parent [-]

Yeah that's a good point, and also takes into account the dreaded DST (what are this business's operating hours for example, which remains the same locally but would change in UTC)

eszed 10 hours ago | parent | prev [-]

I mean... That's kinda how it works? More than once I've halfway forgotten birthdays of friends who live in timezones to my east, and then sent them a message saying "Happy birthday! (It still is where I am, lol)".

I'm not necessarily defending the implementation, just pointing out another way in which time is irreducibly ambiguous and cursed.

layman51 3 hours ago | parent | next [-]

You reminded me of some riddle I had once read that was about trying to figure out how someone could be born one year later but still be older than someone born in previous year. The answer to the riddle also relies on timezones. For sure, birthdates involve time zones.

The riddle explanation was something like: A baby is born in New York City at 12:15 AM on January 1. Thirty minutes later, another baby is born in Los Angeles, where the local time is 9:45 PM on December 31. Although the New York baby is actually older by 30 minutes, the calendar dates make it appear as though the Los Angeles baby was born first.

WorldMaker 3 hours ago | parent [-]

The other biggest fun trick of timezone math to a riddle like that would be the International Date line where a baby born on one side of it can be born on the "day before" by calendar reckoning despite being born 30 minutes after the other side of the line.

hdjrudni 8 minutes ago | parent | next [-]

Isn't that precisely the same?

Doesn't even have to be the International Date line, any two timezones work.

rmunn 2 hours ago | parent | prev [-]

Fraternal (not identical) twins, born aboard a ship traveling west to east across the Pacific. One of them officially born January 1st, 2016. The younger-by-30-minutes twin officially born December 31st, 2015. They'll have the hardest time persuading people that they're really twins once they're grown up.

hdjrudni 6 minutes ago | parent [-]

This works even without timezones. If they're born even a second apart, it can so happen on different days (if they're born around midnight)

whiskey-one 10 hours ago | parent | prev [-]

A reminder associated with the birthday can and should be changed if I change time zones. But the birthday date didn’t change so it shouldn’t move to a different day.

skissane 9 hours ago | parent [-]

> But the birthday date didn’t change so it shouldn’t move to a different day.

But it does. My brother moved to the US for a few years. So we’d send him birthday wishes on the day of his birthday (Australia time), and he’d get them the day before his birthday (his time). Now he’s moved back to Australia, the same thing happens in reverse-he gets birthday wishes from his American friends the day after his birthday.

My wife has lots of American friends on Facebook (none of whom she knows personally, all people she used to play Farmville with)-and she has them wishing her a happy birthday the day after her birthday too. Maybe she’s doing the same to them in reverse.

thayne 3 hours ago | parent [-]

But using UTC doesn't solve that, unless the recipient of the birthday wishes is close to the prime meridian.

teiferer 11 hours ago | parent | prev | next [-]

> 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.)

tshaddox 11 hours ago | parent | next [-]

I agree with the "don't break the web" design principle, but I sometimes disagree with precisely where TC39 draws the line. There is obviously a cost to breaking old, unchanging websites. But there's also a cost to allowing old, unchanging websites to hold the entire web hostage. Balancing those costs is a subjective matter.

As far as I know, TC39 doesn't have any clear guidelines about how many websites or how many users must be affected in order to reject a proposed change to JavaScript behavior. Clearly there are breaking changes that are so insignificant that TC39 should ignore them (imagine a website with some JavaScript that simply iterates over every built-in API and crashes if any of them ever change).

marcosdumay 9 hours ago | parent | prev | next [-]

Browsers should version their languages. They should say "if you use <html version="5.2"> or bigger, this is the behavior".

Somehow, the standard groups decided to remove the versioning that was there.

cogman10 4 hours ago | parent [-]

The decided not to have it there because they didn't like the idea of maintaining version 4.0 forever in their engines.

That's basically why they never did anything like "use strict" again.

IMO, that's a bad choice. Giving yourself the ability to have new behavior and features based on a version is pretty natural and how most programming languages evolve. Having perpetual backwards and fowards compatibility at all times is both hard to maintain and makes it really hard to fix old mistakes.

The only other reason they might have chosen this route is because it's pretty hard to integrate the notion of compatibility levels into minifiers.

mejutoco 10 hours ago | parent | prev [-]

Have an optional parameter to opt in to the old behaviour and keep the new correct behaviour the default (without the parameter) seems like a decent choice.

stevula 9 hours ago | parent [-]

To preserve backwards compatibility and not require all those old sites to update, the legacy behavior would have to be the default, with opt-in for the new behavior.

mejutoco 6 hours ago | parent [-]

That is the opposite approach. Also an option. One could also deprecate the call without parameter and force always a parameter with which behaviour. The deprecation could last enough time that those websites would have been rewritten multiple times ;)

sfink 5 hours ago | parent [-]

The control interface burned into your hardware device will not have been rewritten. And it's not like you can have a flag day where everyone switches over, so the lifespan of those hardware devices isn't that relevant.

Backwards compatibility is a large part of the point of the Web.

You could version everything at whatever granularity you like, but over time that accumulates ("bug 3718938: JS gen24 code incorrectly handles Date math as if it were gen25-34", not to mention libraries that handle some versions but not others and then implicitly pass their expectations around via the objects they create so your dependency resolver has to look at the cross product of versions from all your depencies...)

Kyro38 10 hours ago | parent | prev | next [-]

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

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

publicdebates 10 hours ago | parent [-]

Guessed 2 of the first 3 questions.

Got to question 4 and gave up:

    new Date("not a date")
    1) Invalid Date
    2) undefined
    3) Throws an error
    4) null
There's literally no way of guessing this crap. It's all random.
dvt 7 hours ago | parent | next [-]

I had no idea we even had an `Invalid Date` object, that's legitimately insane. Some other fun ones:

    new Date(Math.E)
    new Date(-1)
are both valid dates lol.
winstonp 9 hours ago | parent | prev | next [-]

the new Date() constructor is an amalgamation of like 5 different specs, and unless the input matches one of them, which one kicks in is up to the implementer's choice

marcosdumay 9 hours ago | parent | prev [-]

The choice here is really surprising. I was half-expecting NaN, that you omitted.

Is there any other instance of the standard JS library returning an error object instead of throwing one? I can't think of any.

jazzyjackson 7 hours ago | parent | next [-]

I think NaN itself is a bit of an error object, especially in how it's passed through subsequent math functions, which is a different choice than throwing up.

But besides that I think you're right, Invalid Date is pretty weird and I somehow never ran into it.

One consequence is you can still call Date methods on the invalid date object and then you get NaN from the numeric results.

WorldMaker 3 hours ago | parent | prev [-]

The fun trick is that Invalid Date is still a Date:

    > let invalid = new Date('not a date')
    > invalid
    Invalid Date
    > invalid instanceof Date
    true
You were half-correct on expecting NaN, it's the low level storage of Invalid Date:

    > invalid.getTime()
    NaN
Invalid Date is just a Date with the "Unix epoch timestamp" of NaN. It also follows NaN comparison logic:

    > invalid === new Date(NaN)
    false
It's an interesting curio directly related to NaN.
netghost 10 hours ago | parent | prev | next [-]

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.

tshaddox 10 hours ago | parent [-]

It's a common source of off-by-one date formatting bugs in client-rendered web apps, particularly ones that pass around "YYYY-MM-DD" date strings (common for OpenAPI JSON APIs).

  const dateStringFromApiResponse = "2026-01-12";
  const date = new Date(dateStringFromApiResponse);
  const formatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'long' });
  formatter.format(new Date("2026-01-12"));

  // 'January 11, 2026'
jazzyjackson 7 hours ago | parent [-]

I'm having flashbacks to writing Power Automate expressions to reconcile Dates passed from Outlook metadata to Excel

Basically just extracting numbers from string index or regex and rearranging them to a string Excel would recognize

sholladay 8 hours ago | parent | prev [-]

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.

sfink 5 hours ago | parent | next [-]

UTC is a fine default time zone, but that doesn't matter here.

A datetime with a timezone and a datetime without one are two different things, both of them useful. My birthday does not have a time zone. My deadline does.

The company deadline for getting some document returned? It might or might not, that's policy.

Poetically: we are born free of time zones. We die bound to one.

lysium 8 hours ago | parent | prev [-]

This will result in incorrect behavior when, between converting to UTC and back to the original timezone, the timezone database has changed, which happens more often than you think.