working days
time period
date calculation
business days
scheduling

Counting regular working days in a given period of time

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Counting working days sounds simple until weekends, holidays, inclusive date ranges, and regional calendars enter the picture. A reliable solution starts by defining what "working day" means in your business context, then encoding that rule explicitly instead of assuming Monday through Friday is always enough.

Define the Rules First

Before writing code, decide:

  • whether the start date is included
  • whether the end date is included
  • which weekdays are considered weekends
  • whether public holidays are excluded
  • whether half-days or custom closures matter

A function that counts Monday through Friday is not the same as a payroll-grade business-day calculator. Ambiguous rules are the main source of bugs here.

A Straightforward Python Implementation

The example below counts inclusive business days, excluding Saturday, Sunday, and a supplied set of holidays.

python
1from datetime import date, timedelta
2
3
4def count_working_days(start_date, end_date, holidays=None):
5    if end_date < start_date:
6        raise ValueError("end_date must be on or after start_date")
7
8    holidays = holidays or set()
9    current = start_date
10    total = 0
11
12    while current <= end_date:
13        is_weekday = current.weekday() < 5
14        is_holiday = current in holidays
15
16        if is_weekday and not is_holiday:
17            total += 1
18
19        current += timedelta(days=1)
20
21    return total
22
23
24holidays = {
25    date(2026, 1, 1),
26    date(2026, 1, 19),
27}
28
29print(count_working_days(date(2026, 1, 1), date(2026, 1, 10), holidays))

This loop is easy to read and correct for many business applications. For normal date ranges such as a month or quarter, the performance is usually fine.

Faster Approaches for Large Ranges

If you need to process many long ranges, iterating day by day may be unnecessary. A faster strategy is:

  1. count full weeks
  2. multiply by the number of working days per week
  3. handle the leftover partial week
  4. subtract matching holidays

That approach is more efficient, but it also requires more care around boundaries and holiday handling. Start with the clear version unless performance measurements prove otherwise.

Handling Holidays Correctly

Holiday logic is often where date-counting code stops being trivial. A holiday list can be:

  • fixed dates
  • rule-based dates such as "third Monday in January"
  • jurisdiction-specific calendars
  • company-specific shutdown dates

For production systems, it is often better to rely on a maintained holiday source than to hard-code a small list. The counting algorithm itself is easy. Keeping the holiday data accurate over time is harder.

Also decide what happens when a holiday falls on a weekend. Some organizations observe the holiday on the following Monday. Others do not. The code needs the business rule, not a guess.

Example with pandas

For data-heavy workflows, pandas can help:

python
1import pandas as pd
2
3holidays = ["2026-01-01", "2026-01-19"]
4business_days = pd.bdate_range(
5    start="2026-01-01",
6    end="2026-01-31",
7    freq="C",
8    holidays=holidays,
9)
10
11print(len(business_days))

This is convenient in analytics code, but it may be overkill for a small application. Choose the dependency level that matches the project.

Time Zones and Date Boundaries

If your inputs are timestamps rather than plain dates, convert them to the business calendar's local date before counting. Otherwise, a timestamp near midnight UTC may fall on the previous or next local day.

For example, an event at 2026-03-11 00:30 UTC is still on 2026-03-10 in parts of North America. If your working-day logic ignores that, counts around deadlines and payroll cutoffs will be wrong.

This is why business-day calculations often belong near domain logic, not in a generic utility that strips time zones away too early.

Common Pitfalls

The most common mistake is forgetting whether the range is inclusive. Counting from Monday to Friday can be five days or four days depending on how the endpoints are defined.

Another mistake is hard-coding Saturday and Sunday as weekends for all users. Some regions and industries use different schedules.

Holiday handling is frequently incomplete. A list of dates copied into code once will become outdated. Treat holiday data as maintained configuration.

Finally, do not use timestamps when you really mean dates. Converting after arithmetic rather than before arithmetic often introduces off-by-one-day errors.

Summary

  • Working-day calculations depend on explicit business rules, not only weekday math.
  • A simple day-by-day loop is often the clearest correct implementation.
  • Large-scale calculations can optimize by counting full weeks and leftover days.
  • Holiday data quality matters as much as the counting code itself.
  • Convert timestamps to the correct local date before applying business-day logic.

Course illustration
Course illustration

All Rights Reserved.