Check if a date is a weekend in JavaScript
Beginner · 3 min read
Delivery estimates, support hours, booking systems and payroll all need to know whether a date falls on a weekend. In this tutorial you will write isWeekend(date), which answers true for Saturday and Sunday. Then you will make a second version where the weekend days are your choice, because not every country rests on the same two days. Finally you will count the weekend days in a whole month.
You need to know how to make a date with new Date(year, month, day) and how a for loop works.
getDay() and the week
getDay() gives the day of the week as a number:
- 0 is Sunday
- 1 is Monday, 2 is Tuesday, 3 is Wednesday, 4 is Thursday
- 5 is Friday
- 6 is Saturday
The week starts at Sunday, so the usual weekend, Saturday and Sunday, sits at the two ends of the range: 6 and 0. They are not next to each other as numbers. Keep that in mind; it matters in Step 2.
Four test dates
16 to 19 January 2026 run from a Friday to a Monday, so they cover a weekday on each side of a weekend:
const friday = new Date(2026, 0, 16);
const saturday = new Date(2026, 0, 17);
const sunday = new Date(2026, 0, 18);
const monday = new Date(2026, 0, 19);
console.log(friday.getDay(), saturday.getDay(), sunday.getDay(), monday.getDay()); // 5 6 0 1
Here is the exercise. Have a go yourself first, or read on and we will build it together.
12 Is this date a weekend?
easyReturn true for a Saturday or Sunday. Then make the weekend configurable, because it is not Saturday and Sunday everywhere.
Stuck? Three hints, smallest first
- 1.
getDay()gives 0 for Sunday and 6 for Saturday, so the weekend is the two ends of the range. - 2. Take the weekend days as an argument, defaulting to
[0, 6]. - 3.
includeson a small array of day numbers is clearer than a chain of||.
Step 1: Saturday or Sunday
Compare the day number with 0 and with 6, joined by || (or):
const isWeekend = (date) => date.getDay() === 0 || date.getDay() === 6;
for (const d of [friday, saturday, sunday, monday]) {
console.log(d.toDateString(), isWeekend(d));
}
// Fri Jan 16 2026 false
// Sat Jan 17 2026 true
// Sun Jan 18 2026 true
// Mon Jan 19 2026 false
Step 2: the tempting shortcut
It looks as if the weekend is "the high numbers", so you might write >= 5:
const wrongWeekend = (date) => date.getDay() >= 5;
console.log(wrongWeekend(friday), wrongWeekend(sunday)); // true false
That catches Friday (5) and Saturday (6), and misses Sunday (0). Sunday is 0, at the other end, so there is no simple range that covers both days. Compare with the two exact numbers instead.
Step 3: let the caller choose the weekend
Saturday and Sunday is not the rule everywhere. In much of the Middle East, the weekend is Friday and Saturday. Whether a day is a weekend is a business rule, not a fact about the date. So pass the weekend days in as an array:
const isWeekendWhere = (date, weekendDays) => weekendDays.includes(date.getDay());
console.log(isWeekendWhere(friday, [5, 6])); // true
console.log(isWeekendWhere(sunday, [5, 6])); // false
console.log(isWeekendWhere(sunday, [0, 6])); // true
includes returns true if the array contains the value. It reads more clearly than a chain of ||, and it works for any number of days.
Step 4: count the weekend days in a month
Loop over each day of the month and count the ones that are weekends. To know when to stop, use the day 0 trick: day 0 of the next month is the last day of this one, and getDate() gives its number:
function weekendDaysIn(year, month) {
const last = new Date(year, month + 1, 0).getDate();
let count = 0;
for (let day = 1; day <= last; day++) {
if (isWeekend(new Date(year, month, day))) count++;
}
return count;
}
console.log(weekendDaysIn(2026, 0)); // 9
console.log(31 - weekendDaysIn(2026, 0)); // 22
January 2026 has 31 days. Five of them are Saturdays and four are Sundays, so nine weekend days and 22 weekdays.
Each date is built from parts with new Date(year, month, day), so it is local midnight and getDay() gives the same answer in every time zone.
The full solution
The panel has all three functions, prints the four test days and the January count, and checks the Friday and Saturday weekend too. Press Run to see it.
const isWeekend = (date) => date.getDay() === 0 || date.getDay() === 6;
// Not universal: much of the Middle East treats Friday and Saturday as the
// weekend, so the days belong to the caller, not to the function.
const isWeekendWhere = (date, weekendDays) => weekendDays.includes(date.getDay());
function weekendDaysIn(year, month) {
const last = new Date(year, month + 1, 0).getDate();
let count = 0;
for (let day = 1; day <= last; day++) {
if (isWeekend(new Date(year, month, day))) count++;
}
return count;
}
// 16 to 19 January 2026: Friday, Saturday, Sunday, Monday.
const friday = new Date(2026, 0, 16);
const saturday = new Date(2026, 0, 17);
const sunday = new Date(2026, 0, 18);
const monday = new Date(2026, 0, 19);
for (const d of [friday, saturday, sunday, monday]) {
console.log(d.toDateString(), '-> weekend:', isWeekend(d));
}
console.log('weekend days in January 2026:', weekendDaysIn(2026, 0));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('Saturday is a weekend', isWeekend(saturday), true);
test('so is Sunday', isWeekend(sunday), true);
test('Friday is not', isWeekend(friday), false);
test('nor is Monday', isWeekend(monday), false);
test('January 2026 has nine weekend days', weekendDaysIn(2026, 0), 9);
test('and 22 weekdays', 31 - weekendDaysIn(2026, 0), 22);
test('with a Friday-Saturday weekend, Friday counts', isWeekendWhere(friday, [5, 6]), true);
test('and Sunday does not', isWeekendWhere(sunday, [5, 6]), false);
test('the default convention is Sunday and Saturday', [sunday.getDay(), saturday.getDay()], [0, 6]);
The mechanics are two comparisons. The design point is the second function: "weekend" is a business rule, not a property of a date, and hard-coding Saturday and Sunday is a bug in any product with users in Egypt, the UAE or Israel. Passing the days in costs nothing and makes the assumption visible.
Note that Sunday and Saturday are 0 and 6 — the two ends of the range, not two adjacent numbers. That is why day >= 5 is wrong: it catches Friday and Saturday, not Saturday and Sunday.
Common mistakes
- Writing
getDay() >= 5. That is Friday and Saturday. Sunday is 0, not 7. - Using
getDate()instead ofgetDay().getDate()is the day of the month, sogetDate() === 6is true on the 6th, whatever day that is. - Hard-coding Saturday and Sunday for everyone. Take the weekend days as an argument when your users live in different countries.
Try it yourself
- Write
weekdaysIn(year, month)that counts the days that are not weekends. - Change
weekendDaysInso it takes aweekendDaysarray and usesisWeekendWhere. Count January 2026 with[5, 6]. - Write
nextWeekday(date)that returns the next date that is not a Saturday or Sunday.