Date is out, Temporal is in

(piccalil.li)

100 points | by alexanderameye 2 hours ago

14 comments

  • tshaddox 4 minutes 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 alter 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...

  • rjrjrjrj 24 minutes 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)
  • xeckr 1 hour 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 hour ago
      To be fair it's exactly the type of stuff I'd be glad if I never have to think about again.
    • ivanjermakov 51 minutes ago
      Go down by half of user traffic, go up 10x of crawler traffic.
  • tensegrist 1 hour 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
  • crabl 1 hour ago
    we've been using this Temporal polyfill and it's been awesome so far: https://github.com/js-temporal/temporal-polyfill
    • whizzter 1 hour ago
      How is it compared to moment and especially luxon?
      • kristo 48 minutes ago
        It’s a web standard api and very small weight. Highly recommend.
  • Aardwolf 1 hour 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 16 minutes 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 hour 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 38 minutes 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)
  • Someone1234 36 minutes 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.

  • moralestapia 43 minutes 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.

  • ckocagil 1 hour 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.
  • zvqcMMV6Zcr 1 hour 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 hour 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.

      • zvqcMMV6Zcr 27 minutes ago
        I just groked it before and thought "plain" in `Temporal.PlainDateTime` indicates some specific format instead of just being zoneless. It is actually always using ISO8601 fur build in toString conversions, so I don't really have anything to complain about.
  • LoganDark 1 hour 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.
  • artursapek 9 minutes ago
    bro these ads I can't
  • sublinear 1 hour 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 37 minutes 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.
  • jdonaldson 42 minutes ago
    [flagged]
    • DarkNova6 12 minutes ago
      What’s the superior alternative and in which domains?