Python Business Duration Calculator

0.68 · active · verified Thu Apr 16

The `business-duration` library calculates the duration between two dates and times, excluding weekends, public holidays, and non-business hours. It provides a single function, `businessDuration`, that can compute results in days, hours, minutes, or seconds. The project is actively maintained with irregular releases, with the latest version (0.68) released in October 2025.

Common errors

Warnings

Install

Imports

Quickstart

This example calculates the business duration in hours between two specific dates, excluding weekends and US public holidays in California, and considering a defined daily business hour range.

from business_duration import businessDuration
import pandas as pd
import holidays as pyholidays
from datetime import time

# Start date must be in standard python datetime format
start_date = pd.to_datetime('2017-07-01 02:02:00')
# End date must be in standard python datetime format
end_date = pd.to_datetime('2017-07-07 04:48:00')

# Business open hour must be in standard python time format (Hour, Min, Sec)
biz_open_time = time(7, 0, 0)
# Business close hour must be in standard python time format (Hour, Min, Sec)
biz_close_time = time(17, 0, 0)

# US public holidays for California
US_holiday_list = pyholidays.US(state='CA')

# Business duration can be 'day', 'hour', 'min', 'sec'
unit_hour = 'hour'

# Calculate and print the business duration
result = businessDuration(
    startdate=start_date,
    enddate=end_date,
    starttime=biz_open_time,
    endtime=biz_close_time,
    holidaylist=US_holiday_list,
    unit=unit_hour
)

print(f"Business duration: {result} {unit_hour}s")
# Expected result: 30.0 hours (July 1st, 2nd are weekends, 4th is a US public holiday. 3 business days * 10 hours/day = 30 Hours).

view raw JSON →