在Python中获取日期范围之间的所有奇数天

时间:2018-11-28 18:24:15

标签: python

我在python中尝试过,在给定的日期范围内,我什至没有奇怪,甚至星期三。例。如果我指定日期范围为02-11-2018至30-11-2018,则需要在python中找到所有奇数的星期四,甚至是星期四

1 个答案:

答案 0 :(得分:0)

from datetime import date, timedelta, datetime

curr = "1-12-2018"
end = "30-12-2018"
format = "%d-%m-%Y"
start_date = datetime.strptime(curr, format)
end_date = datetime.strptime(end, format)

step = timedelta(1)
num_wed = 0
off_days = ['Wed']

days = (end_date - start_date).days
for x in range(days):
    day = start_date.strftime("%a")
    if day in off_days:
        num_wed += 1
        if((num_wed%2!=0)):
                date = start_date.strftime("%d-%m-%Y")
                print(date)
    start_date += step

print(num_wed)

现在工作正常