0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

python get business days between two dates

Last updated at Posted at 2023-01-20
import holidays
from datetime import datetime
import pandas as pd

HOLIDAYS_US = holidays.US()
def GetBusinessDays(start, end):
    startTime = datetime.strptime(start, '%Y-%m-%d')
    endTime = datetime.strptime(end, '%Y-%m-%d')
    dates = pd.date_range(startTime,endTime,freq='D')

    res = []
    for date in dates:
        if date.weekday() in holidays.WEEKEND or date in HOLIDAYS_US:
            continue
        res.append(date)
    return res

if __name__ == '__main__':
    dates=GetBusinessDays('2020-03-18','2023-01-19')
    print(dates)

NextBusinessDay

from datetime import datetime, timedelta
import holidays
import pytz

HOLIDAYS_US = holidays.US()

def GetNextBusinessDay(days=1):
    next_day = datetime.now(pytz.timezone('America/Chicago'))
    if (
        next_day.hour >= 15 or 
        next_day.hour < 9 or 
        next_day.hour == 9 and next_day.minute <= 30
    ): days += 1
    days = timedelta(days=days)
    next_day = next_day.date() + days
    while next_day.weekday() in holidays.WEEKEND or next_day in HOLIDAYS_US:
        next_day += days
    return next_day
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?