简体   繁体   中英

TypeError: can't compare datetime.datetime to datetime.date

I have the following code

minDate = date(9999, 12, 31)
start = event.get('dtstart').dt
if isinstance(start, datetime.datetime):
        newStart = start.date()
else:
   newStart = start
if(newStart < minDate):
        minDate = start



why am I getting this error as I have converted to date on both ends of the comparison

They are not the same type. From Docs:

class datetime.date - An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: year, month, and day.

class datetime.datetime - A combination of a date and a time. Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo

You will need to mitigate the comparison here: if(newStart < minDate):

Where minDate is date type and newStart is dateTime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM