Yes, someone actually did this and I found it running on our server
Image Transcription: Meme
[Paneled meme with a brain that gets increasingly glowing]
[The brain is smaller than the skull]
if dayOfWeek.name == "Sunday" || dayOfWeek.name == "Saturday"
weekend = true
[The brain is glowing in some areas]
if dayOfWeek < 2 || dayOfWeek > 6
weekend = true
[The brain is shooting out rays of light]
weekend = !((dayOfWeek - 1) % 6)
I am a human volunteer who transcribes posts to improve accessibility on programming.dev and you could be one too! !transcribing@programming.dev
Depending on whether this code is in a hotpath (and considering how “elementary” it is, I figure that’s a possibility), this could very well be a significant speed improvement.
Though I’d say that only excuses it if it’s truly an elementary function (and not one line as part of a larger function), as otherwise it’s unreadable garbage. But on its own it:
- has a clear purpose
- (presumably) isn’t reimplementing functionality
- is easily tested
- can be modified with no side effects (besides breaking your calendar, but that’s beside the point)
weekend = dayOfWeek > 5
Sunday is 1 0 and Saturday is 7 6. You just made Friday part of the weekend You forgot Sunday
I was off by one, but Sunday is 0 in javascript
Both Monday and Sunday are used as the first day of the week with quite some regularity. It’s a completely arbitrary standard no different to "the tenth month is the one called “October”. Or dividing a day into 24 segments which are each broken into 60 smaller segments of 60 even smaller segments. You can’t say either is “wrong” per se.
Personally, I was brought up learning Sunday is the first day of the week, but at some point decided that was bullshit partly because it’s the week end. But also just from a practical standpoint when looking at a calendar, it’s useful to have the weekend days grouped together.
You forgot weekend = dayOfWeek.name[0] == ‘S’;
Can confirm this works completely as expected when the user’s system is set to lang=ES.
Simple save the users language setting in a variable, change it to english, check if the first letter is “s” and then change the language back.
Ok another US local units are retarded rant: it’s called weekEND! why do you start your week at sunday and not monday! Sunday is part of the weekEND!
I’m English, not American but I see it as Saturday and Sunday are the two ends of the week. Like how a string has two ends. The weekend is both the start and the finishing end of the week.
since we are in a temporal context here i would argue that there is a clear distrinction between beginning and end here
So, when someone asks if you are free the next two weekends, you assume they’re talking about the next Saturday (tail weekend) and the next Sunday (front weekend)?
If you’re referring to an “end” of an object, it can refer to the extreme of a side of it. For example, aglets are at either end of a shoelace.