148
submitted 2 weeks ago by [email protected] to c/[email protected]

cross-posted from: https://programming.dev/post/33754840

new Date("wtf")

top 25 comments
sorted by: hot top new old
[-] [email protected] 29 points 2 weeks ago

Best laugh I've had in a while. That is some grade-A jank.

[-] [email protected] 14 points 2 weeks ago
[-] [email protected] 3 points 2 weeks ago

I have now, that's fantastic!

[-] [email protected] 1 points 2 weeks ago
[-] [email protected] 23 points 2 weeks ago

This quiz was stressful. Like, there were so many times when I knew I was being cued up for a trick question, but I still fell for it.

[-] [email protected] 11 points 2 weeks ago

It's a mess. Expectation is always wrong, basically.

[-] [email protected] 20 points 2 weeks ago* (last edited 2 weeks 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.

[-] [email protected] 18 points 2 weeks ago

Damn, I thought you meant JavaScript itself was being replaced.

[-] [email protected] 16 points 2 weeks ago

One can only undefined

[-] [email protected] 7 points 2 weeks 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...

[-] [email protected] 14 points 2 weeks ago

It is awesome, in the same way a planet killer asteroid heading straight for us is awesome!

[-] [email protected] 14 points 2 weeks ago* (last edited 2 weeks ago)

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

Love this quiz btw

[-] [email protected] 9 points 2 weeks ago

Luckily the new Temporal API is shaping up.

[-] [email protected] 2 points 2 weeks ago

I quite like this one.

[-] [email protected] 9 points 2 weeks ago

Ah, JavaScript...

[-] [email protected] 4 points 2 weeks ago

Until the new Temporal API comes along, there are some libraries we can use.

  • luxon
  • dayjs
  • or compile chrono (Rust) to WebAssembly
[-] [email protected] 4 points 2 weeks 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?

[-] [email protected] 13 points 2 weeks ago

Parsing user input? Nonsense data coming from an API?

[-] [email protected] 1 points 2 weeks ago* (last edited 2 weeks ago)

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

[-] [email protected] 4 points 2 weeks ago

Maybe you don't expect shit data; you just get it. I know, always expect it....

[-] [email protected] 7 points 2 weeks 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.

[-] [email protected] 4 points 2 weeks ago

You think you'd get an error from the constructor.

[-] [email protected] 2 points 2 weeks 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.

[-] [email protected] 0 points 2 weeks 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...

[-] [email protected] 2 points 2 weeks ago

Because reasonable APIs have input validation provided out of the box, so that not everybody has to reinvent the validation wheel (inevitably incorrectly).

this post was submitted on 12 Jul 2025
148 points (99.3% liked)

Programmer Humor

37499 readers
522 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS