- 代码
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() |
- 如果计算相隔年,或相隔月份,相隔天数,可以修改规则
# 间隔秒数
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