• kinttach@lemmy.zip
    link
    fedilink
    arrow-up
    11
    ·
    edit-2
    6 hours ago

    In the process of being replaced.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal

    JavaScript has had the Date object for handling date and time since its first days. However, the Date API is based on the poorly designed java.util.Date class from Java, which was replaced in the early 2010s; but, because of JavaScript’s goal of backward compatibility, Date sticks around in the language.

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      1
      ·
      3 hours ago

      Hmm, I can believe that it was based on java.util.Date, but I don’t remember that being as unpredictable. I guess, a different API to begin with, would have avoided a lot of problems, though…

  • Dessalines@lemmy.ml
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    4 hours ago

    You do not, under any circumstances, “gotta hand it to javascript”.

    Love this quiz btw

  • mogoh@lemmy.ml
    link
    fedilink
    arrow-up
    3
    arrow-down
    1
    ·
    8 hours ago

    Ok, I get that the Date API is problematic, but I wouldn’t expect anything meaningfull from new Date("not a date").getTime() anyway. Why would you in the first place?

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      2
      ·
      2 hours ago

      It’s mainly horrid, because it means you have to code extremely defensively (or I guess, use a different API).
      You can’t rely on new Date("not a date") aborting execution of your function by throwing an error. Instead, you have to know that it can produce an Invalid Date object and check for that. Otherwise a random NaN shows up during execution, which is gonna be extremely fun to try to find the source of.

      I understand that it’s implemented like that partially for historical reasons, partially because it’s often better to display “NaN” rather than nothing, but it’s still the sort of behavior that puts me in a cold sweat, because I should be memorizing all kinds of Best Practices™ before trying to code JavaScript.

    • WildPalmTree@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      3 hours ago

      Because this is a simplified example? Maybe you create the object in one place (saying something more realistic like “2015” or whatever your inexperience or AI told you to) and use getTime() at a later place where you thought you created it in a correct way.

      • Ephera@lemmy.ml
        link
        fedilink
        English
        arrow-up
        1
        arrow-down
        1
        ·
        3 hours ago

        (saying something more realistic like “2015” or whatever your inexperience or AI told you to)

        User input is probably the big one where this API is gonna get stress-tested…

      • pixely@lemmy.world
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        edit-2
        5 hours ago

        If you’re expecting shit data then you’d have unit tests for those cases so you’d know what to expect.