Search code examples
pythondatedate-comparisondelta

Comparing date in python and applying a condition on comparison


i want to compare/subtract two date's due date from current date in python and apply a condition on this subtraction that if the difference is >0 days then calculate fine by multiplying difference with fine per day

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)

Solution

  • You need to use the timedelta function in the datetime module:

    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))