python – How to compare two dates?

The Question :

409 people think this question is useful

How would I compare two dates to see which is later, using Python?

For example, I want to check if the current date is past the last date in this list I am creating, of holiday dates, so that it will send an email automatically, telling the admin to update the holiday.txt file.

The Question Comments :
  • Er, you use the < and > operators, just like with any other comparison.
  • @JohnMachin: you write a function with prototype int compare_dates(void const *, void const*), cast both arguments to struct Date * and implement the comparison logic. It may not be that obvious to a Python newcomer.
  • @larsmans: Sorry …. s/any_language/any_reasonable_language/ and anyone used to an unreasonable language should spend a few minutes perusing the docs and trying out date1 < date2
  • docs.python.org/library/datetime.html#datetime-objects Ctrl-F search for “Supported operations”
  • @Galaxy – he’s using an analogy, to show that what should be simple to a seasoned person might not be simple to a total beginner and he’s using C to illustrate this. He doesn’t think the question is about C. And he was right, I was a total beginner at the time I asked this question almost 8 years ago, and had no clue how any of it worked.

The Answer 1

522 people think this answer is useful

Use the datetime method and the operator < and its kin.

>>> from datetime import datetime, timedelta
>>> past = datetime.now() - timedelta(days=1)
>>> present = datetime.now()
>>> past < present
True
>>> datetime(3000, 1, 1) < present
False
>>> present - datetime(2000, 4, 4)
datetime.timedelta(4242, 75703, 762105)

The Answer 2

79 people think this answer is useful

Use time

Let’s say you have the initial dates as strings like these:
date1 = "31/12/2015"
date2 = "01/01/2016"

You can do the following:
newdate1 = time.strptime(date1, "%d/%m/%Y") and newdate2 = time.strptime(date2, "%d/%m/%Y") to convert them to python’s date format. Then, the comparison is obvious:

newdate1 > newdate2 will return False
newdate1 < newdate2 will return True

The Answer 3

41 people think this answer is useful

datetime.date(2011, 1, 1) < datetime.date(2011, 1, 2) will return True.

datetime.date(2011, 1, 1) - datetime.date(2011, 1, 2) will return datetime.timedelta(-1).

datetime.date(2011, 1, 1) + datetime.date(2011, 1, 2) will return datetime.timedelta(1).

see the docs.

The Answer 4

4 people think this answer is useful

Other answers using datetime and comparisons also work for time only, without a date.

For example, to check if right now it is more or less than 8:00 a.m., we can use:

import datetime

eight_am = datetime.time( 8,0,0 ) # Time, without a date

And later compare with:

datetime.datetime.now().time() > eight_am  

which will return True

The Answer 5

-2 people think this answer is useful

For calculating days in two dates difference, can be done like below:

import datetime
import math

issuedate = datetime(2019,5,9)   #calculate the issue datetime
current_date = datetime.datetime.now() #calculate the current datetime
diff_date = current_date - issuedate #//calculate the date difference with time also
amount = fine  #you want change

if diff_date.total_seconds() > 0.0:   #its matching your condition
    days = math.ceil(diff_date.total_seconds()/86400)  #calculate days (in 
    one day 86400 seconds)
    deductable_amount = round(amount,2)*days #calclulated fine for all days

Becuase if one second is more with the due date then we have to charge

Add a Comment