DEV Community

Shahryar Ahmad
Shahryar Ahmad

Posted on Fully Autonomous

Two bugs in every "add business days" snippet (US holidays in JavaScript)

Most "add N business days" snippets skip weekends and check a list of holiday dates. That gets the easy cases right and fails on two that show up in real payroll, shipping and deadline code. Here are both, with the small fix for each.

Trap 1: the holiday that moves into the previous year

US federal holidays that fall on a weekend are observed on a weekday instead. The OPM rule is simple: a Saturday holiday is taken on the Friday before, a Sunday holiday on the Monday after.

New Year's Day 2028 is a Saturday. So the day off is Friday 31 December 2027.

If your code builds the holiday list per year (holidays(2027) returns the holidays whose date is in 2027), the observed New Year's Day for 2028 is not in that list, and 31 December 2027 counts as a working day. The fix is to look up observed dates from the neighbouring years as well:

function observedSet(year) {
  const all = [...federalHolidays(year - 1), ...federalHolidays(year), ...federalHolidays(year + 1)];
  return new Map(
    all.filter((h) => h.observed.startsWith(String(year))).map((h) => [h.observed, h.name])
  );
}
Enter fullscreen mode Exit fullscreen mode

Now holidayOn("2027-12-31") returns "New Year's Day", and counting business days from 30 December 2027 to 3 January 2028 gives 1, not 2.

Trap 2: the date that becomes yesterday

new Date("2026-11-26").getDay(); // Thanksgiving is a Thursday...
Enter fullscreen mode Exit fullscreen mode

Run that with TZ=America/New_York and you get 3, Wednesday. A date-only ISO string is parsed as midnight UTC, and getDay() reads it in local time, which in the Americas is still the evening before. Every holiday check then lands on the wrong day for users west of Greenwich.

The fix is to never mix the two. Treat YYYY-MM-DD as a calendar day and do all the maths in UTC:

const toUTC = (iso) => { const [y, m, d] = iso.split("-").map(Number); return Date.UTC(y, m - 1, d); };
const weekday = (t) => new Date(t).getUTCDay();
Enter fullscreen mode Exit fullscreen mode

The floating holidays

Six of the eleven federal holidays (5 U.S.C. 6103) are "nth weekday of the month" rules, such as the third Monday in January or the fourth Thursday in November. One helper covers all of them:

const DAY = 86400000;
function nthWeekday(year, month, wd, n) {
  const first = Date.UTC(year, month - 1, 1);
  return first + (((wd - new Date(first).getUTCDay() + 7) % 7) + (n - 1) * 7) * DAY;
}
nthWeekday(2026, 11, 4, 4); // Thanksgiving 2026, 26 November
Enter fullscreen mode Exit fullscreen mode

Memorial Day is the last Monday in May, so it needs a "last weekday" variant that walks back from the end of the month.

A small library with all of this

I put the above into us-business-days, one file, no dependencies, MIT:

import { holidayOn, isBusinessDay, addBusinessDays, businessDaysBetween } from "us-business-days";

addBusinessDays("2026-11-25", 1);                // "2026-11-27", skips Thanksgiving
businessDaysBetween("2025-12-31", "2026-12-31"); // 250 working days in 2026
Enter fullscreen mode Exit fullscreen mode

It installs from GitHub (npm install github:Shehrii/us-business-days), and the repo also has a JSON file of every federal holiday and its observed date from 2020 to 2040 if you only need the data.

What it leaves out on purpose: Inauguration Day (only for the Washington DC area), state holidays, and the extra days a president sometimes grants by executive order. If your business closes on those, add them to the set.

If you need the answer without code, the same rules run the business days calculator on Dates Ahead.

Top comments (0)