在python中比较日期并在比较中应用条件

时间:2019-05-23 11:23:06

标签: python date date-comparison delta

我想在python中从当前日期比较/减去两个日期的到期日期,并对该减法应用一个条件,如果差异> 0天,则通过将差异乘以每天罚款来计算罚款

from datetime import date

a=date.today()

issuedate=date(2019,5,9)

duedate#should be 5 days after issue date i can't find the method for doing this 

check=a-duedate

#  if check>0days:
#   print(check days*40)

2 个答案:

答案 0 :(得分:0)

您需要在datetime模块中使用timedelta函数:

from datetime import date
import datetime

a=date.today()

issuedate=date(2019,5,9)

duedate = issuedate+datetime.timedelta(days=5)

check=a-duedate
print(check>=datetime.timedelta(days=0))

答案 1 :(得分:0)

使用时间增量

从日期时间导入timedelta

duedate = issuedate + timedelta(days=5)

check=(a-duedate).days