我是 python 日期和时间类型的新手。
我有一个日期值。
date = '2018-11-10 10:55:31+00:00'
我需要检查此日期值是否早于 90 天。
我试过 :
from datetime import datetime
from datetime import timedelta
past = datetime.now() - timedelta(days=90)
date = '2018-11-10 10:55:31+00:00'
if past > date :
print("This is older than 90 days")
失败并出现以下错误:
TypeError: '>' not supported between instances of 'datetime.datetime' and 'str'
这可能是因为“过去”的日期格式和我传递的日期值不同。
我怎么能想出这个?
最佳答案
您必须使用 strptime
将字符串转换为日期。
比较运算符仅适用于日期时间。
date = datetime.strptime('2018-11-10 10:55:31', '%Y-%m-%d %H:%M:%S')
那你能做吗
if past > date :
print("This is older than 90 days")
关于python - 类型错误 : '>' not supported between instances of 'datetime.datetime' and 'str' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53248537/