0

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?

  • 1
    Make sure you are aware of which data type you (have to) use where in the code; string ≠ datetime object. `strftime` formats date(time) objects to string. You cannot do timedelta arithmetic with strings. remove `.strftime('%Y,%m,%d')`. – FObersteiner Apr 16 '21 at 07:00
  • Can timedelta understand if the format is in ('%d-%m-%Y') format? – Stressemann Apr 16 '21 at 07:03
  • No. datetime or timedelta objects have no *format*, only the string representation of date/time/timedelta has a certain format. – FObersteiner Apr 16 '21 at 07:04

1 Answers1

0

You can't add a timedelta to a str, which is what you are doing. This line: date1 = datetime.datetime.strptime(d1, '%d-%m-%Y').strftime('%Y,%m,%d') creates a datetime obj, and then converts it back to str.

Change your code like so and it will work:

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')
                date2 = datetime.datetime.strptime(d2, '%d-%m-%Y')
                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")


if __name__ == '__main__':
    daterange()
MikeHunter
  • 4,144
  • 1
  • 19
  • 14