Add business days to a date in JavaScript
Advanced · 4 min read
"Delivery in 5 working days." "Invoice due 10 business days after receipt." When an order is placed on a Friday, five working days later is the next Friday, not the Wednesday. Getting this right matters for delivery dates, payment terms and support deadlines.
In this tutorial you will write addBusinessDays(date, count). It moves a date forward by a number of weekdays, skipping Saturdays and Sundays, and goes backwards when the count is negative. You need to know getDay(), which gives 0 for Sunday and 6 for Saturday, and that setDate changes a date in place.
Moving a date by one day
setDate(getDate() + 1) moves a date to the next day. If that goes past the end of the month, JavaScript rolls over into the next month for you:
const d = new Date(2026, 0, 31);
d.setDate(d.getDate() + 1);
console.log(d.getMonth(), d.getDate()); // 1 1
31 January plus one day is 1 February (month 1, day 1). So we can step one day at a time without thinking about month lengths.
The plan
Calendar days and business days are different counts. The idea is simple: take one step at a time, and only count the step if you landed on a weekday. Keep going until you have counted enough.
Here is the exercise. Try it on your own first if you like, then read on and we will build it together.
47 Add business days, skipping weekends
hard"Five working days from Friday" should be the following Friday, not the Wednesday. Write addBusinessDays, and make negative counts go backwards.
Stuck? Three hints, smallest first
- 1. Step one day at a time and only count down when the day you land on is a weekday.
- 2. Adding 7 to the count and dividing by 5 does not work: the answer depends on which day you start.
- 3. Zero should return the same date, even if it is a Saturday.
Step 1: two small helpers
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
const isWeekend = (date) => date.getDay() === 0 || date.getDay() === 6;
const friday = new Date(2026, 0, 16);
console.log(iso(friday), isWeekend(friday)); // 2026-01-16 false
iso shows a date as YYYY-MM-DD so results are easy to read. isWeekend checks for Sunday (0) or Saturday (6).
Step 2: the wrong turn, adding calendar days
The first idea is to add the days directly:
const naive = new Date(friday);
naive.setDate(naive.getDate() + 1);
console.log(iso(naive), isWeekend(naive)); // 2026-01-17 true
One business day after Friday came out as Saturday. A common "fix" is to treat 5 business days as 7 calendar days. That works only when you start on a weekday and the jump fits neatly into whole weeks. It breaks for other counts, for weekend starts and for negative counts.
Step 3: step and count
function addBusinessDays(date, count) {
const copy = new Date(date);
const step = count < 0 ? -1 : 1;
let remaining = Math.abs(count);
while (remaining > 0) {
copy.setDate(copy.getDate() + step);
if (!isWeekend(copy)) remaining--;
}
return copy;
}
console.log(iso(addBusinessDays(friday, 1))); // '2026-01-19'
console.log(iso(addBusinessDays(friday, 5))); // '2026-01-23'
Line by line:
new Date(date)makes a copy, so the caller's date is never changed.stepis1to go forward or-1to go back.remainingis how many business days are still to go.Math.absremoves the minus sign.- Each time round the loop, we move first, then decide whether that day counted.
Friday plus 1 is Monday the 19th. Friday plus 5 is the next Friday, the 23rd.
Step 4: why "move first" matters
Start on a Saturday:
const saturday = new Date(2026, 0, 17);
console.log(iso(addBusinessDays(saturday, 1))); // '2026-01-19'
The first step lands on Sunday, which doesn't count. The second lands on Monday, which does, so the loop stops.
What if the loop checked the day before moving? Then it would count the day you start on:
function checkThenMove(date, count) {
const copy = new Date(date);
let remaining = count;
while (remaining > 0) {
if (!isWeekend(copy)) remaining--;
copy.setDate(copy.getDate() + 1);
}
return copy;
}
console.log(iso(checkThenMove(friday, 1))); // '2026-01-17', a Saturday
Friday counted as the first business day, and the result is the Saturday after it. Moving first avoids that.
Step 5: going backwards and zero
console.log(iso(addBusinessDays(new Date(2026, 0, 19), -1))); // '2026-01-16'
console.log(iso(addBusinessDays(saturday, 0))); // '2026-01-17'
console.log(iso(friday)); // '2026-01-16'
One business day before Monday the 19th steps back over the weekend to Friday the 16th. A count of zero returns the same date, even on a Saturday. If you want "the next working day", that is a different question and deserves its own function with a clear name.
The last line shows friday itself hasn't changed, thanks to the copy.
Step 6: no time-zone surprises here
This loop moves a local date with setDate, which can cause bugs elsewhere. Here it is safe. The loop stops when a counter reaches zero, not by comparing two moments in time. setDate always keeps the calendar day correct, and getDay only reads the day, so a clock change that moves the hour can't break it.
console.log(iso(addBusinessDays(new Date(2026, 0, 12), 10))); // '2026-01-26'
Ten business days from Monday 12 January is Monday 26 January, two weeks on.
The full solution
The panel has iso, isWeekend and addBusinessDays. It prints steps from a Friday, a Saturday and a Monday. The checks cover the weekend jump, two weeks, a Saturday start, negative counts, zero, and that the input is not changed. Press Run to see them.
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
const isWeekend = (date) => date.getDay() === 0 || date.getDay() === 6;
function addBusinessDays(date, count) {
const copy = new Date(date);
const step = count < 0 ? -1 : 1;
let remaining = Math.abs(count);
while (remaining > 0) {
copy.setDate(copy.getDate() + step); // move first, then decide whether it counted
if (!isWeekend(copy)) remaining--;
}
return copy;
}
// January 2026: Mon 12, Fri 16, Sat 17, Mon 19, Fri 23, Mon 26.
const friday = new Date(2026, 0, 16);
console.log('Fri 16 + 1 ->', iso(addBusinessDays(friday, 1)), '<- Monday');
console.log('Fri 16 + 5 ->', iso(addBusinessDays(friday, 5)), '<- the next Friday');
console.log('Sat 17 + 1 ->', iso(addBusinessDays(new Date(2026, 0, 17), 1)));
console.log('Mon 19 - 1 ->', iso(addBusinessDays(new Date(2026, 0, 19), -1)), '<- back to Friday');
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('one business day past a Friday is the Monday', iso(addBusinessDays(friday, 1)), '2026-01-19');
test('five is the following Friday, not the Wednesday', iso(addBusinessDays(friday, 5)), '2026-01-23');
test('five from a Monday is the Monday after', iso(addBusinessDays(new Date(2026, 0, 12), 5)), '2026-01-19');
test('ten is two weeks on', iso(addBusinessDays(new Date(2026, 0, 12), 10)), '2026-01-26');
test('starting on a Saturday, one lands on the Monday', iso(addBusinessDays(new Date(2026, 0, 17), 1)), '2026-01-19');
test('a negative count goes backwards over the weekend', iso(addBusinessDays(new Date(2026, 0, 19), -1)), '2026-01-16');
test('zero returns the same date, weekend or not', iso(addBusinessDays(new Date(2026, 0, 17), 0)), '2026-01-17');
test('the result of a positive count is never a weekend', isWeekend(addBusinessDays(friday, 3)), false);
test('and the input is not modified', iso(friday), '2026-01-16');
The loop moves first and counts second, which is what makes the weekend-start case work: from a Saturday, the first step lands on Sunday and counts nothing, the second lands on Monday and counts one.
The arithmetic shortcut people reach for — five business days is seven calendar days — is only right when you start on a weekday and land before the following weekend. The Saturday and negative cases break it, which is why the assertions include both. Zero deliberately returns the date unchanged rather than "the next working day"; if you need that behaviour, make it a separate function with a name that says so.
This local cursor is safe where the one in exercise 46 was not, and the difference is worth naming: here the loop ends when a counter reaches zero, so it never compares one instant against another. setDate keeps the calendar day correct even when a clock change moves the hour, and getDay only ever reads the day. Comparing instants is what introduced the daylight-saving hazard, not stepping through days.
Common mistakes
- Treating 5 business days as 7 calendar days. It fails for other counts, for weekend starts and for negative counts. Step and count instead.
- Changing the caller's date.
setDateworks in place. Copy withnew Date(date)first. - Checking the day before moving. From a Saturday you then count the wrong days. Move first, then check.
Try it yourself
- Add a
holidayslist and skip those dates as well as weekends. - Write
nextWorkingDay(date)that returns the date itself if it is a weekday, or the following Monday if not. - Print the delivery date for an order placed each day from Monday 12 to Sunday 18 January 2026 with 3-day delivery.