Comment by tshaddox

Comment by tshaddox a day ago

58 replies

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 19 hours 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

  • WorldMaker 14 hours ago

    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 12 hours ago

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

      • phplovesong 7 hours ago

        PHP suffers from this too. By too strict BC PHP has become a really mess of a language. IIRC there still is the ad-hoc parameter order convention and lack of any namespacing for builtins. Everything is global.

        With JS i kind of get it as you cant control the env. Bit PHP does not have this limitation, and they still cant fix the mess that is PHP.

  • josephg 13 hours ago

    > 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.
  • abuob 9 hours ago

    I find it very unfortunate that browsers (or rather, the spec) do not support some kind of versioning. If we could declare which version of HTML, JS and CSS to use, it would allow for breaking changes without breaking the entire web.

    There are so many (in hindsight bad) design choices and implementation accidents that currently exist in perpetuity because of backwards compatibility; the web would really benefit if every now and then we could shed old baggage.

    • bazoom42 5 hours ago

      It would also force browsers to implement multiple slightly different engine modes, vastly complicating the browser code.

      There are already a few cases, eg quirks mode vs standards mode and “use strict” mode, which was considered necessary for moving forward, but clearly it also complicates things for browsers. We dont want more modes than what is necessary.

  • agos 3 hours ago

    directives sort of kinda work if you squint the eyes, but only as a crutch and only if you can't/don't want to change the API.

    > 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';`

    This is what happened here, only the API changed as well

  • hnlmorg 16 hours ago

    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 14 hours ago

      > 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 14 hours ago

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

  • [removed] 14 hours ago
    [deleted]
OptionOfT 21 hours 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...

  • abustamam 20 hours ago

    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.

    • mjevans 13 hours ago

      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 12 hours ago

        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)

    • mexicocitinluez 4 hours ago

      This has been a huge source of frustration in C#.

      The BCL-provided DateTime was really confusing, especially when you just needed a Date. They eventually got around to including a DateOnly, but before that happened I switched to a library called "Noda" (or Joda in Java) and after a bit of a learning curve, it made everything a lot easier to reason about.

      It has LocalDates and LocalDateTimes, as well as Instants to store UTC times. It also offers ZonedDateTimes, but I don't use those as much. I work in healthcare. And so many regulations involve strictly dates. Like, "You have 5 days to do X", not "You have 120 hours to do X", and as such, storing the time with a lot of this data can add more complexity.

  • eszed 21 hours ago

    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.

    • whiskey-one 21 hours ago

      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 20 hours ago

        > 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 14 hours ago

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

    • layman51 14 hours ago

      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 14 hours ago

        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.

teiferer a day 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.)

  • tshaddox a day ago

    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 20 hours ago

    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 15 hours ago

      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.

  • dahauns 5 hours ago

    Nah, that's not a "sacrifice", but the only sane way. In the ideal case, clearly document the constructor with a warning that it's not ISO conformant and offer a ISO conformant alternative.

    In my (unfortunate) experience, DateTime/Timezone handling is one of the things most prone to introduce sneaky, but far-reaching bugs as it is. Introducing such a behaviour change that (usually) won't fail-fast, will often seemingly continue working as before until it doesn't and is deceptively tricky to debug/pinpoint/fix ist just asking for a fast lane into chaos.

    And even with JS going the extra mile on backwards compatibility, I don't think most other languages would introduce that kind of breaking change in that way either.

  • mejutoco 21 hours ago

    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 20 hours ago

      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 17 hours ago

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

Kyro38 21 hours ago

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

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

  • publicdebates 21 hours ago

    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 19 hours ago

      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 21 hours ago

      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 20 hours ago

      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 18 hours ago

        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 14 hours ago

        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.
        • tyilo 7 hours ago

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

          This is just because a JS Date is an object and have nothing to do with the inner representation.

              > new Date(0) === new Date(0)
              false
GoblinSlayer an hour 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.

netghost 21 hours 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.

  • tshaddox 21 hours ago

    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 18 hours ago

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

  • sfink 16 hours ago

    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.

    • throwaway290 7 hours ago

      > My birthday does not have a time zone. My deadline does.

      That seems subjective

  • lysium 19 hours ago

    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.

cush 8 hours ago

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

[removed] 8 hours ago
[deleted]