简体   繁体   English

类型错误:无法将 datetime.datetime 与 datetime.date 进行比较

[英]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. class datetime.date - 一个理想化的天真日期,假设当前的公历一直有效,并且永远有效。 Attributes: year, month, and day.属性:年、月、日。

class datetime.datetime - A combination of a date and a time. class datetime.datetime - 日期和时间的组合。 Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo属性:年、月、日、时、分、秒、微秒、tzinfo

You will need to mitigate the comparison here: if(newStart < minDate):您需要在此处减轻比较: if(newStart < minDate):

Where minDate is date type and newStart is dateTime其中minDatedate类型, newStartdateTime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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