比较日期是否比其他日期大两个月 Python
Compare if a date is two months bigger than other with Python
我一直在尝试使用 python 来比较某个日期是否比其他日期大两个月。
有人知道我做错了什么吗?
提前致谢
import datetime
from datetime import datetime, timedelta
import time
dateEfec = "01/01/2016"
dateBase = "01/03/2016"
effectivedate = time.strptime(dateEfec,"%d/%m/%Y")
baseline = time.strptime(dateBase, "%d/%m/%Y")
calc = effectivedate > baseline + relativedelta(months=2)
print(calc)
我收到这个错误:
类型错误:只能将元组(不是“relativedelta”)连接到元组
timedelta
没有属性 'month' - 因为一个月是一个模糊的数量,它可以有 28-31 天。请改用 relativedelta。
from datetime import datetime
from dateutil import relativedelta
dateEfec = "01/03/2016"
dateBase = "01/01/2016"
effectivedate = datetime.strptime(dateEfec,"%d/%m/%Y")
baseline = datetime.strptime(dateBase, "%d/%m/%Y")
calc = effectivedate >= baseline + relativedelta.relativedelta(months=2)
print(calc)
# True
注意:我修改了示例,以便更好地说明。
我一直在尝试使用 python 来比较某个日期是否比其他日期大两个月。
有人知道我做错了什么吗?
提前致谢
import datetime
from datetime import datetime, timedelta
import time
dateEfec = "01/01/2016"
dateBase = "01/03/2016"
effectivedate = time.strptime(dateEfec,"%d/%m/%Y")
baseline = time.strptime(dateBase, "%d/%m/%Y")
calc = effectivedate > baseline + relativedelta(months=2)
print(calc)
我收到这个错误:
类型错误:只能将元组(不是“relativedelta”)连接到元组
timedelta
没有属性 'month' - 因为一个月是一个模糊的数量,它可以有 28-31 天。请改用 relativedelta。
from datetime import datetime
from dateutil import relativedelta
dateEfec = "01/03/2016"
dateBase = "01/01/2016"
effectivedate = datetime.strptime(dateEfec,"%d/%m/%Y")
baseline = datetime.strptime(dateBase, "%d/%m/%Y")
calc = effectivedate >= baseline + relativedelta.relativedelta(months=2)
print(calc)
# True
注意:我修改了示例,以便更好地说明。