欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python计算两日期之间工作日时长

程序员文章站 2022-02-19 06:33:42
...

1. 代码: 

def exclude_weekend_time():
    from dateutil import rrule

    start_date = datetime.datetime.strptime('2019-04-19 13:21:46', '%Y-%m-%d %H:%M:%S')
    end_date = datetime.datetime.strptime('2019-04-22 13:21:46', '%Y-%m-%d %H:%M:%S')

    days_off = 5, 6
    workdays = [x for x in range(7) if x not in days_off]
    secondly = rrule.rrule(rrule.SECONDLY, dtstart=start_date, until=end_date, byweekday=workdays)
    seconds = secondly.count()
    print(seconds)


if __name__ == '__main__':
    exclude_weekend_time()

 

2. 如果计算相隔年,或相隔月份,相隔天数,可以修改规则

# 间隔秒数
secondly = rrule.rrule(rrule.SECONDLY, dtstart=start_date, until=end_date, byweekday=workdays)

# 间隔分钟数
secondly = rrule.rrule(rrule.MINUTELY, dtstart=start_date, until=end_date, byweekday=workdays)

# 间隔小时数
secondly = rrule.rrule(rrule.HOURLY, dtstart=start_date, until=end_date, byweekday=workdays)

# 间隔天数
secondly = rrule.rrule(rrule.DAILY, dtstart=start_date, until=end_date, byweekday=workdays)

... ...

 

参考:https://blog.csdn.net/houyj1986/article/details/21880331