this post was submitted on 06 Nov 2023
31 points (69.1% liked)

Programmer Humor

32472 readers
949 users here now

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

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 9 points 1 year ago* (last edited 1 year ago) (1 children)

date.setDate(1);

Problem solved.

[–] [email protected] -1 points 1 year ago (2 children)

Sure, but the question is why anybody thought this would be a desirable behavior in the first place.

[–] [email protected] 17 points 1 year ago (1 children)

It's because there's no right answer, and this way gets you the intuitive answer most often.

A month isn't a proper unit of time. Adding a month to a date can't be done without specifying which month you're adding.

You could argue that one month from January 31 is February 28, 29 (depending on the year), March 2, or 3.

Should one month from the last day be the last day of the next month? That would mean that the 30th and the 31st of march are both the same duration from April 30th, and a month before April 30th could logically map to either one.

So they chose the path that, for anything other than the 31st, and the 29th and 30th if it comes near February, works as you expect. "A month after 17 days from the first of January is 17 days after the first of february.”

The other alternatives involve not allowing the addition and subtraction of irregular time intervals, but then you get frustrated that you can only deal with seconds, since those don't change in length.

[–] [email protected] 4 points 1 year ago (2 children)

I love js. But the date object has always been a total pain. Moment.js is a good package to deal with it, but yeah, it's currently deprecated, but it would be nice if it or something like it became part of ECMAScript.

I have no idea why it hasn't yet, except that it might be that js needs to work for everyone, not just the us. So time is not standard.

[–] [email protected] 3 points 1 year ago* (last edited 1 year ago) (1 children)

The date API is like the original rip of the Java date API. Barely changed, and totally backwards compatible nonsense.

Temporal is the new JavaScript/ECMAScript date API.
It's stage 3, and likely stable (just a few kinks being worked out). So you could polyfill it for production.
https://github.com/tc39/proposal-temporal

[–] [email protected] 2 points 1 year ago

Speaking of Java RipS. How annoying is it the JS has left Java in the dust as far as looser standards?

Developing in Java: YOU FORGOT A SEMI-COLON ARE YOU CRAZY?! HOW IS THE COMPILER SUPPOSED TO KNOW WHAT TO DO?!

Developing in JS: Who gives a fuck about semi-colons?

[–] [email protected] 2 points 1 year ago

You may be interested in the datefns library if you need a replacement for momentjs.