Python-排除两个日期之间的周末 [英] Python - Exclude weekends between two Dates
本文介绍了Python-排除两个日期之间的周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算两个日期之间的差额,但想从中排除周末。日期格式如下:
I want to calculate the difference between the two dates but want to exclude the weekends from it . Below is the format of dates :
CreateDate - 2017-08-29 10:47:00
ResolveDate - 2017-09-23 16:56:00
推荐答案
您可以使用 numpy.busday_count:
You can use numpy.busday_count:
from datetime import datetime
import numpy as np
create_date = "2017-08-29 10:47:00"
resolve_date = "2017-09-23 16:56:00"
create_datetime = datetime.strptime(create_date, '%Y-%m-%d %H:%M:%S')
resolve_datetime = datetime.strptime(resolve_date, '%Y-%m-%d %H:%M:%S')
print(f"The difference in days is: {(resolve_datetime - create_datetime).days}")
print(f"The difference in business days is: {np.busday_count(create_datetime.date(), resolve_datetime.date())}")
输出:
The difference in days is: 25
The difference in business days is: 19
这篇关于Python-排除两个日期之间的周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文