How to calculate business days in JavaScript
Date math in JS is famously sharp-edged; business-day math more so.
1. Plain JS (UTC-safe skeleton)
function addBusinessDays(iso, n, holidays = new Set()) {
const d = new Date(iso + "T00:00:00Z");
const step = Math.sign(n); let left = Math.abs(n);
while (left > 0) {
d.setUTCDate(d.getUTCDate() + step);
const dow = d.getUTCDay();
const key = d.toISOString().slice(0, 10);
if (dow !== 0 && dow !== 6 && !holidays.has(key)) left--;
}
return d.toISOString().slice(0, 10);
}
Works, for Saturday and Sunday countries, with a holiday set you maintain. Other weekends, substitute-day rules, and market calendars are where it quietly breaks.
2. One fetch
const r = await fetch(
`https://<host>/v1/add?region=SA&start=2027-01-06&days=2`,
{ headers: { "X-Api-Key": KEY } });
(await r.json()).result // "2027-01-10", Fri-Sat weekend handledNeed this in code? The API answers the same questions over HTTPS with one GET request. Free tier of 100 calls a day, and the playground needs no key at all.