37 votes

new Date("wtf"); How well do you know JavaScript's Date class?

20 comments

  1. [3]
    macleod
    Link
    perfectly sane language for perfectly sane people

    perfectly sane language for perfectly sane people

    13 votes
    1. [2]
      Barney
      Link Parent
      Similarly: null == 0; // false null >= 0; // true null > 0; // false null <= 0; // true null < 0; // false 1 / null; // Infinity

      Similarly:

      null == 0; // false
      
      null >= 0; // true
      null > 0;  // false
      
      null <= 0; // true
      null < 0;  // false
      
      1 / null;  // Infinity
      
      7 votes
      1. macleod
        Link Parent
        Check out this list, some incredibly perfect things for the most special and perfect language there is, love that it powers practically the world: wtfjs

        Check out this list, some incredibly perfect things for the most special and perfect language there is, love that it powers practically the world: wtfjs

  2. Zorind
    Link
    Okay that was great. Thanks, I feel like I learned something that I will immediately forget and has no practical use, because when would I want to rely on any of that functionality.

    Okay that was great.

    Thanks, I feel like I learned something that I will immediately forget and has no practical use, because when would I want to rely on any of that functionality.

    12 votes
  3. [5]
    alp
    Link
    I work with this language every single day, and new Date(“maybe 1”) evaluating to 11pm on 30 April 2001 may be one of the most upsetting pieces of news that I’ve ever been delivered. I’ll be...

    I work with this language every single day, and new Date(“maybe 1”) evaluating to 11pm on 30 April 2001 may be one of the most upsetting pieces of news that I’ve ever been delivered. I’ll be putting in my notice at work this morning.

    12 votes
    1. [3]
      marcus-aurelius
      Link Parent
      It's annoying that is not even consistent, on Firefox returns "Invalid Date"

      It's annoying that is not even consistent, on Firefox returns "Invalid Date"

      5 votes
      1. [2]
        alp
        Link Parent
        I’m going to cry. Separately, am I being silly here in not understanding why Invalid Date date objects even exist? Why not just throw an error?

        I’m going to cry.

        Separately, am I being silly here in not understanding why Invalid Date date objects even exist? Why not just throw an error?

        4 votes
        1. smores
          Link Parent
          This is very common in JavaScript. Almost no browser APIs throw errors, instead we get undefined, NaN, and Invalid Date. I think that the underlying principle is that JavaScript was first designed...

          This is very common in JavaScript. Almost no browser APIs throw errors, instead we get undefined, NaN, and Invalid Date. I think that the underlying principle is that JavaScript was first designed to allow some minimal amount of scripting on top of the DOM, and it's "better" for website viewers if the date widget shows up but reads "Invalid Date", then if all of the website's interactivity breaks because of a Date math error.

          10 votes
    2. CannibalisticApple
      Link Parent
      So, how would you define the end of your notice period using the Date class? c:

      So, how would you define the end of your notice period using the Date class? c:

  4. HiddenTig
    Link
    Got like 8 wrong in a row and rage quit. I actually like js but man it has to be one of the oddest non-esoteric languages in existence.

    Got like 8 wrong in a row and rage quit. I actually like js but man it has to be one of the oddest non-esoteric languages in existence.

    8 votes
  5. smores
    Link
    For all of the non-JS devs in here completely baffled by how monstrously bad the JavaScript Date interface is... We know haha. TC39 has been working on a new set of APIs for time in JavaScript for...

    For all of the non-JS devs in here completely baffled by how monstrously bad the JavaScript Date interface is... We know haha. TC39 has been working on a new set of APIs for time in JavaScript for ten years. It's rolling out across the ecosystem now, actually. It launched in Firefox last year, in Chrome last month, and is being worked on actively in Safari. Last month's Chrome launch means that it will likely be available soon in Node.js as well. Deno actually unflagged it two years ago. Seems like Bun is lagging behind a bit but I imagine that they'll race to catch up once Node.js releases it.

    Anyway, all that to say, there are a lot of really smart and passionate folks that are trying very hard to make JavaScript better, and Temporal is going to end up being one of the better date and time interfaces across programming languages. And it should start being more broadly available by the end of this year, which is huge!

    8 votes
  6. [2]
    Aldehyde
    Link
    Managed to get 13 right without knowing JS, but this is truly insane. What even are these type coercions. Truly glad to not be a webdev lol.

    Managed to get 13 right without knowing JS, but this is truly insane. What even are these type coercions. Truly glad to not be a webdev lol.

    7 votes
    1. 0xSim
      Link Parent
      The JS Date object is so awful that no sane developer uses it beyond the most trivial use cases. Most of us use wrapper libraries, like Moment.js or date-fns.

      The JS Date object is so awful that no sane developer uses it beyond the most trivial use cases. Most of us use wrapper libraries, like Moment.js or date-fns.

      3 votes
  7. CannibalisticApple
    Link
    I got 11 right somehow despite being very rusty in JavaScript. Nice way to highlight how convoluted the date system is. I was starting to fear it would manually count all months after the start...

    I got 11 right somehow despite being very rusty in JavaScript. Nice way to highlight how convoluted the date system is.

    I was starting to fear it would manually count all months after the start date one by one... And that may actually be more logical than how it actually works.

    4 votes
  8. [5]
    Narry
    Link
    This only reminds me why I refuse to code in JavaScript when I can avoid it. So much web design can happen without that sloppy language. I'll write in something else and let it cross-compile into...

    This only reminds me why I refuse to code in JavaScript when I can avoid it. So much web design can happen without that sloppy language. I'll write in something else and let it cross-compile into JS for me, thanks.

    3 votes
    1. [4]
      NonoAdomo
      Link Parent
      I've learned quite a few languages over the years and JavaScript is by far the most frustrating work of coding I've ever had the displeasure to work with. I actively avoid it whenever possible...

      I've learned quite a few languages over the years and JavaScript is by far the most frustrating work of coding I've ever had the displeasure to work with. I actively avoid it whenever possible because it's just so nonsensical.

      3 votes
      1. [3]
        Narry
        Link Parent
        What’s worse to me is that Microsoft TypeScript purports to fix all the issues that JavaScript has, but it just introduces its own issues. Every time I end up looking at TypeScript code, it’s rife...

        What’s worse to me is that Microsoft TypeScript purports to fix all the issues that JavaScript has, but it just introduces its own issues. Every time I end up looking at TypeScript code, it’s rife with the “any” variable type. Sure, let’s fix all the problems that JavaScript has, but leave a back door to use them just in case. It’s meant to be used judiciously by an experienced programmer, but that’s never stopped a novice from banging on, in my experience. At least this was true the last time I looked at it, but I haven’t bothered to investigate TS in some years.

        3 votes
        1. [2]
          stu2b50
          Link Parent
          Even in non-transpired languages, you’re allowed to cast things to the most generic base type and essentially opt out of the type system. Even in C, that’s what void pointers are for. It’s not...

          Even in non-transpired languages, you’re allowed to cast things to the most generic base type and essentially opt out of the type system. Even in C, that’s what void pointers are for.

          It’s not really an option not to have.

          7 votes