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