📌  相关文章
📜  Python程序获取两个日期之间的总工作日

📅  最后修改于: 2022-05-13 01:54:27.650000             🧑  作者: Mango

Python程序获取两个日期之间的总工作日

给定两个日期,我们的任务是编写一个Python程序来获取总工作日,即两个日期之间的工作日。

例子:

方法一:使用timedelta() + sum() + weekday()

在这种情况下,所有日期都是使用 timedelta() 提取的,通过增加差异直到结束日期。之后,工作日使用 weekday() 进行过滤,将所有值小于 5 的值相加。即周一至周五。

Python3
# Python3 code to demonstrate working of
# Business days in range
# Using timedelta() + sum() + weekday()
from datetime import datetime, timedelta
  
# initializing dates ranges
test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 7, 1)
  
# printing dates
print("The original range : " + str(test_date1) + " " + str(test_date2))
  
# generating dates
dates = (test_date1 + timedelta(idx + 1)
         for idx in range((test_date2 - test_date1).days))
  
# summing all weekdays
res = sum(1 for day in dates if day.weekday() < 5)
  
# printing
print("Total business days in range : " + str(res))


Python3
# Python3 code to demonstrate working of
# Business days in range
# Using np.busday_count
from datetime import datetime, timedelta
import numpy as np
  
# initializing dates ranges 
test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 7, 1)
  
# printing dates 
print("The original range : " + str(test_date1) + " " + str(test_date2))
  
# generating total days using busday_count()
res = np.busday_count(test_date1.strftime('%Y-%m-%d'),
                      test_date2.strftime('%Y-%m-%d'))
  
# printing 
print("Total business days in range : " + str(res))


Python3
# Python3 code to demonstrate working of
# Business days in range
# Using pd.bdate_range
from datetime import datetime
import pandas as pd
  
# initializing dates ranges
test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 6, 30)
  
# printing dates
print("The original range : " + str(test_date1) + " " + str(test_date2))
  
# generating total days using pd.bdate_range()
# len() gets the number of days
# includes both last and first date.
res = len(pd.bdate_range(test_date1.strftime('%Y-%m-%d'),
                         test_date2.strftime('%Y-%m-%d')))
  
# printing result
print("Total business days in range : " + str(res))


输出:

The original range : 2015-06-03 00:00:00 2015-07-01 00:00:00
Total business days in range : 20

方法 #2:使用 np.busday_count()

这是内置函数,可用于直接应用来解决此任务。



蟒蛇3

# Python3 code to demonstrate working of
# Business days in range
# Using np.busday_count
from datetime import datetime, timedelta
import numpy as np
  
# initializing dates ranges 
test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 7, 1)
  
# printing dates 
print("The original range : " + str(test_date1) + " " + str(test_date2))
  
# generating total days using busday_count()
res = np.busday_count(test_date1.strftime('%Y-%m-%d'),
                      test_date2.strftime('%Y-%m-%d'))
  
# printing 
print("Total business days in range : " + str(res))

输出:

The original range : 2015-06-03 00:00:00 2015-07-01 00:00:00
Total business days in range : 20

方法#3:使用pandas.bdate_range()

这是另一个内置函数,可用于直接应用来解决此任务。返回总营业日期列表,包括开始和结束日期。

蟒蛇3

# Python3 code to demonstrate working of
# Business days in range
# Using pd.bdate_range
from datetime import datetime
import pandas as pd
  
# initializing dates ranges
test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 6, 30)
  
# printing dates
print("The original range : " + str(test_date1) + " " + str(test_date2))
  
# generating total days using pd.bdate_range()
# len() gets the number of days
# includes both last and first date.
res = len(pd.bdate_range(test_date1.strftime('%Y-%m-%d'),
                         test_date2.strftime('%Y-%m-%d')))
  
# printing result
print("Total business days in range : " + str(res))

输出 :

The original range : 2015-06-03 00:00:00 2015-06-30 00:00:00
Total business days in range : 20