I've tried following examples such as https://www.pythonprogramming.in/get-range-of-dates-between-specified-start-and-end-date.html and Python generating a list of dates between two dates with various slightly different versions of the same code:
import datetime
from datetime import date, timedelta
def daterange():
while True:
d1 = input("Please enter in the earliest date: ")
check1 = d1.replace("-","")
if check1.isdigit:
d2 = input("Please enter in the latest date: ")
check2 = d2.replace("-","")
if check2.isdigit:
date1 = datetime.datetime.strptime(d1, '%d-%m-%Y').strftime('%Y,%m,%d')## this is to convert my intended input format
date2 = datetime.datetime.strptime(d2, '%d-%m-%Y').strftime('%Y,%m,%d')## this is to convert my intended input format
print (date1)
print (date2)
dd = [date1 + datetime.timedelta(days=x) for x in range (0,(date2-date1).days)]
print (dd)
return
else:
print("That was an invalid date")
else:
print("That was an invalid date")
And I'm getting the same error where it cannot do a subtraction in date2-date1
:
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Can anyone explain why and provide a solution?