Why counting business days between dates requires walking the calendar, not math
A developer discovered that the common shortcut of dividing total days by 7 and multiplying by 5 breaks down whenever a date range does not span exact full weeks. To solve this accurately, they built a workday calculator that iterates through each day and checks whether it falls within a user-defined work week, rather than assuming a fixed five-day schedule. The tool handles custom work weeks — such as four-day weeks or schedules including Saturday — by storing workdays as a set and testing each day for membership. For the reverse problem of finding a date a given number of workdays away, the logic similarly walks the calendar day by day, decrementing a counter only on valid workdays. The developer also flagged a subtle bug risk when mixing date libraries, as JavaScript's native Date object parses ISO date strings as UTC while returning weekdays in local time, which can cause off-by-one errors.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in