正如我在网站上搜索以获取python中的工作日数一样,但是我也需要工作日的日期。
我的输入将是:
start date = 01-03-2019
end_date = 15-03-2019
days = monday , tuesday
预期的输出:
我需要打印星期一和星期二的数量以及日期。
答案 0 :(得分:1)
尝试一下:
start_date = '01-03-2019' # Considering 03 is the month, 01 is the day
end_date = '15-03-2019' # Considering 03 is the month, 15 is the day
start_date = [int(i) for i in start_date.split("-")]
end_date = [int(i) for i in end_date.split("-")]
days = 'monday' , 'tuesday'
from datetime import timedelta, date
start_date = date(start_date[-1], start_date[1], start_date[0])
end_date = date(end_date[-1], end_date[1], end_date[0])
# Now check in the range of start_date and end_date with condition .weekday() Monday is 0 and Tuesday is 1.
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + timedelta(n)
for single_date in daterange(start_date, end_date):
if single_date.weekday() ==0:
print("Monday : ", single_date)
if single_date.weekday() == 1:
print("Tuesday : ", single_date)
答案 1 :(得分:1)
使用pandas.to_datetime
和pandas.date_range
:
import pandas as pd
start_date = '01-03-2019'
end_date = '15-03-2019'
days = ['Monday', 'Tuesday']
dates = pd.to_datetime([start_date, end_date], format='%d-%m-%Y')
pd.date_range(dates[0],dates[1],freq='1d').day_name().value_counts()[days]
Monday 2
Tuesday 2
dtype: int64
答案 2 :(得分:1)
start_date = datetime.strptime(start_date, '%Y-%m-%d')
end_date = datetime.strptime(send_date, '%Y-%m-%d')
step = timedelta(days=1)
while start_date <= end_date:
for day in days:
if day == calendar.day_name[start_date.date().weekday()]:
print calendar.day_name[start_date.date()]
start_date += step