snippetjavascriptTip
Determine if a JavaScript date is a weekday or weekend
Viewed 0 times
javascriptdatedetermineweekdayweekend
Problem
I've often found myself needing to check if a given date is a weekday or weekend. As I'm not particularly fond of using libraries for such simple tasks, I've come up with a couple of simple helper functions to do the job.
The entire premise of these snippets is quite simple. We use
The entire premise of these snippets is quite simple. We use
Date.prototype.getDay() to get the day of the week, which is a number between 0 and 6, where 0 is Sunday and 6 is Saturday. We then use the modulo operator (%) to check if the day is a multiple of 6, which is the case for both Saturday and Sunday. If the remainder is 0, we know the date is a weekend. If the remainder is not 0, we know the date is a weekday.Solution
const isWeekday = date => date.getDay() % 6 !== 0;
const isWeekend = date => date.getDay() % 6 === 0;
isWeekday(new Date('2024-01-05')); // true
isWeekend(new Date('2024-01-05')); // false
isWeekday(new Date('2024-01-06')); // false
isWeekend(new Date('2024-01-06')); // trueCode Snippets
const isWeekday = date => date.getDay() % 6 !== 0;
const isWeekend = date => date.getDay() % 6 === 0;
isWeekday(new Date('2024-01-05')); // true
isWeekend(new Date('2024-01-05')); // false
isWeekday(new Date('2024-01-06')); // false
isWeekend(new Date('2024-01-06')); // trueContext
From 30-seconds-of-code: date-is-weekday-or-weekend
Revisions (0)
No revisions yet.