python获取两个日期间的工作日

时间:2021-04-24 15:51:38
import datetime
# 计算两个日期之间的工作日数,非天数.
class workDays():
def __init__(self, start_date, end_date, days_off=None):
"""days_off:休息日,默认周六日, 以0(星期一)开始,到6(星期天)结束, 传入tupple
没有包含法定节假日,
"""
self.start_date = start_date
self.end_date = end_date
self.days_off = days_off
if self.start_date > self.end_date:
self.start_date,self.end_date = self.end_date, self.start_date
if days_off is None:
self.days_off = 5,6
# 每周工作日列表
self.days_work = [x for x in range(7) if x not in self.days_off] def workDays(self):
"""实现工作日的 iter, 从start_date 到 end_date , 如果在工作日内,yield 日期
"""
# 还没排除法定节假日
tag_date = self.start_date
while True:
if tag_date > self.end_date:
break
if tag_date.weekday() in self.days_work:
yield tag_date
tag_date += datetime.timedelta(days=1) def daysCount(self):
"""工作日统计,返回数字"""
return len(list(self.workDays())) def weeksCount(self, day_start=0):
"""统计所有跨越的周数,返回数字
默认周从星期一开始计算
"""
day_nextweek = self.start_date
while True:
if day_nextweek.weekday() == day_start:
break
day_nextweek += datetime.timedelta(days=1)
# 区间在一周内
if day_nextweek > self.end_date:
return 1
weeks = ((self.end_date - day_nextweek).days + 1)/7
weeks = int(weeks)
if ((self.end_date - day_nextweek).days + 1)%7:
weeks += 1
if self.start_date < day_nextweek:
weeks += 1
return weeks

获取结果:

import datetime
startdate = datetime.datetime(2018,1,11,20,20,20)
enddate = datetime.datetime(2019,1,11,20,20,20)
work = workDays(startdate,enddate) # 需要传入两个datetime格式日期
print(list(work.workDays())) # 获取一个元素为datetime日期格式的工作日期列表
print(work.daysCount()) # 获取工作日期的天数
print(work.weeksCount()) # 获取非工作日的天数
for i in work.workDays(): # 获取每一个工作日期
print(i)
print(dir(work)) # 获取work的所有方法