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

37 points by macleod 20 hours ago on tildes | 20 comments

[OP] macleod | 20 hours ago

perfectly sane language for perfectly sane people

Barney | 6 hours ago

Similarly:

<span class="syntax-kc">null</span> <span class="syntax-o">==</span> <span class="syntax-mf">0</span><span class="syntax-p">;</span> <span class="syntax-c1">// false</span>

<span class="syntax-kc">null</span> <span class="syntax-o">>=</span> <span class="syntax-mf">0</span><span class="syntax-p">;</span> <span class="syntax-c1">// true</span>
<span class="syntax-kc">null</span> <span class="syntax-o">></span> <span class="syntax-mf">0</span><span class="syntax-p">;</span>  <span class="syntax-c1">// false</span>

<span class="syntax-kc">null</span> <span class="syntax-o"><=</span> <span class="syntax-mf">0</span><span class="syntax-p">;</span> <span class="syntax-c1">// true</span>
<span class="syntax-kc">null</span> <span class="syntax-o"><</span> <span class="syntax-mf">0</span><span class="syntax-p">;</span>  <span class="syntax-c1">// false</span>

<span class="syntax-mf">1</span> <span class="syntax-o">/</span> <span class="syntax-kc">null</span><span class="syntax-p">;</span>  <span class="syntax-c1">// Infinity</span>

[OP] macleod | 2 hours ago

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

Zorind | 19 hours ago

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.

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.

marcus-aurelius | 15 hours ago

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

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?

smores | 8 hours ago

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.

CannibalisticApple | 3 hours ago

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

battybattybat | 19 hours ago

Obligatory link (one of my favorite ever lightning talks): https://www.destroyallsoftware.com/talks/wat

HiddenTig | 17 hours ago

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.

smores | 8 hours ago

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!

Aldehyde | 15 hours ago

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.

0xSim | 15 hours ago

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.

CannibalisticApple | 14 hours ago

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.

Narry | 16 hours ago

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.

NonoAdomo | 9 hours ago

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.

Narry | 4 hours ago

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.

stu2b50 | 4 hours ago

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.

Minori | 3 hours ago

Even Rust has unsafe in some core libraries!