如何在“datetime.timedelta”和“int”之间进行操作?

问题描述 投票:0回答:1

我正在尝试在 datetime.timedelta 和 int 之间执行“>”。这是我的代码:

import datetime
today = datetime.date.today()      
date = int(input("Enter date"))
month = int(input("Enter month"))
year= int(input("Enter year"))
date1= datetime.date(year,month,date)
difference=today-date1
print((difference.days),"days")
if (difference)>14:
    print("The difference is not allowed greater than 14")

我的输入是正确的,但系统显示我无法比较“datetime.timedelta”和“int”。

Enter date04
Enter month04
Enter year2020
36 days
Traceback (most recent call last):
  File "C:\Users\khooz\OneDrive\Documents\test.py", line 9, in <module>
    if (difference)>14:
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'

我怎样才能在这两个对象之间做“>”?谢谢你的回答!!XD

python datetime int operation
1个回答
0
投票

如何在这两个对象之间执行“>”操作?

要比较

timedelta
int
对象,您需要将一个对象转换为另一个对象:

if difference > datetime.timedelta(days=14)

if difference.days > 14
© www.soinside.com 2019 - 2024. All rights reserved.