▲ 31 ▼ If you run this JavaScript function on the 31st of a month, the result will be a month off. The best part is that this is the intended behvaior. JavaScript is a cursed language. (lemmy.ml) submitted 2 years ago by yogthos@lemmy.ml to c/programmerhumor@lemmy.ml 47 comments fedilink hide all child comments function getMonthName(monthNumber) { const date = new Date(); date.setMonth(monthNumber - 1); return date.toLocaleString([], { month: 'long' }); } The current day of month will have an impact on the behavior of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date. For example, if the current value is 31st January 2016, calling setMonth with a value of 1 will return 2nd March 2016. This is because in 2016 February had 29 days.
[+] railsdev@programming.dev 2 points 2 years ago* (last edited 2 years ago) (4 children) [deleted] permalink fedilink source parent hideshow 4 child comments replies: [–] ricecake@sh.itjust.works 1 point 2 years ago (2 children) So, the flip side to that is that sometimes you need to add one month to a date, because that sometimes how human systems are written. By not providing a function that does that, you're just pushing the confusion down to the developer, who is more likely to make terrible errors in the process, get frustrated, or use one of N different competing libraries, each of which chose a different answer. Omitting functionality that can behave unintuitively in certain circumstances means leaving out a lot of functionality that people need. Like, "decimal numbers" go pathological in certain cases. So do Unicode characters. Don't even bother thinking about connecting to the network. permalink fedilink source parent hideshow 2 child comments replies: [+] railsdev@programming.dev 0 points 2 years ago* (last edited 2 years ago) (1 child) [deleted] permalink fedilink source parent hideshow 1 child comment replies: [–] ricecake@sh.itjust.works 2 points 2 years ago Why wouldn't you just use the ruby functions for adding a month to a date? https://ruby-doc.org/stdlib-2.5.1/libdoc/date/rdoc/Date.html#method-i-3C-3C It seems really weird that there's so much pushback against "date time math is tricky, read the manual to find out exactly which compromise your library chose". permalink fedilink source parent [–] yogthos@lemmy.ml [S] -5 points 2 years ago Exactly, it's better to not have these sorts of "conveniences" than to create weird pitfalls. I find a lot of crazy Js behaviors are ultimately a result of Js trying to be accommodating of inputs that should just be straight up rejected. permalink fedilink source parent
[–] ricecake@sh.itjust.works 1 point 2 years ago (2 children) So, the flip side to that is that sometimes you need to add one month to a date, because that sometimes how human systems are written. By not providing a function that does that, you're just pushing the confusion down to the developer, who is more likely to make terrible errors in the process, get frustrated, or use one of N different competing libraries, each of which chose a different answer. Omitting functionality that can behave unintuitively in certain circumstances means leaving out a lot of functionality that people need. Like, "decimal numbers" go pathological in certain cases. So do Unicode characters. Don't even bother thinking about connecting to the network. permalink fedilink source parent hideshow 2 child comments replies: [+] railsdev@programming.dev 0 points 2 years ago* (last edited 2 years ago) (1 child) [deleted] permalink fedilink source parent hideshow 1 child comment replies: [–] ricecake@sh.itjust.works 2 points 2 years ago Why wouldn't you just use the ruby functions for adding a month to a date? https://ruby-doc.org/stdlib-2.5.1/libdoc/date/rdoc/Date.html#method-i-3C-3C It seems really weird that there's so much pushback against "date time math is tricky, read the manual to find out exactly which compromise your library chose". permalink fedilink source parent
[+] railsdev@programming.dev 0 points 2 years ago* (last edited 2 years ago) (1 child) [deleted] permalink fedilink source parent hideshow 1 child comment replies: [–] ricecake@sh.itjust.works 2 points 2 years ago Why wouldn't you just use the ruby functions for adding a month to a date? https://ruby-doc.org/stdlib-2.5.1/libdoc/date/rdoc/Date.html#method-i-3C-3C It seems really weird that there's so much pushback against "date time math is tricky, read the manual to find out exactly which compromise your library chose". permalink fedilink source parent
[–] ricecake@sh.itjust.works 2 points 2 years ago Why wouldn't you just use the ruby functions for adding a month to a date? https://ruby-doc.org/stdlib-2.5.1/libdoc/date/rdoc/Date.html#method-i-3C-3C It seems really weird that there's so much pushback against "date time math is tricky, read the manual to find out exactly which compromise your library chose". permalink fedilink source parent
[–] yogthos@lemmy.ml [S] -5 points 2 years ago Exactly, it's better to not have these sorts of "conveniences" than to create weird pitfalls. I find a lot of crazy Js behaviors are ultimately a result of Js trying to be accommodating of inputs that should just be straight up rejected. permalink fedilink source parent