1 回答
TA贡献1773条经验 获得超3个赞
您需要更新两者的范围daterange并daterange_2包含所有必要的日期:
import datetime
# First date range is next week from monday to sunday:
next_week_day = datetime.datetime.now().date() + datetime.timedelta(days=7)
monday = next_week_day - datetime.timedelta(days=next_week_day.weekday())
sunday = monday + datetime.timedelta(days=7)
daterange = [monday + datetime.timedelta(days=x) for x in range(0, (sunday - monday).days + 1)]
# Second daterange can be anything:
start = datetime.date(2020, 8, 2)
end = datetime.date(2020, 9, 25)
daterange_2 = [start + datetime.timedelta(days=x) for x in range(0, (end - start).days + 1)]
# then I create a list to add matching days to create the new range
date_list = []
for date in daterange:
for date_2 in daterange_2:
if date_2 == date:
date_list.append(date)
# and retrieve first and last day in list
first_date = date_list[0]
second_date = date_list[-1]
print(first_date, second_date)
print(type(first_date), type(second_date))
添加回答
举报