553

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.

7
  • 30
    Er, you use the < and > operators, just like with any other comparison. Commented Nov 15, 2011 at 20:00
  • 12
    @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. Commented Nov 15, 2011 at 20:42
  • 1
    @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 Commented Nov 15, 2011 at 20:51
  • 2
    docs.python.org/library/datetime.html#datetime-objects Ctrl-F search for "Supported operations" Commented Nov 15, 2011 at 20:56
  • 1
    @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. Commented May 7, 2019 at 3:34

7 Answers 7

689

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) 
Sign up to request clarification or add additional context in comments.

9 Comments

This works for timezone-aware values as well, if anyone was wondering.
What's different between past and present? I can't understand your example and its result doesn't make sense.
@Emadpres: imagine this was typed manually. The past line was typed first, while the present line was typed second... so the past line was entered first, so past < present is True.
Quoting from the doc: "If one comparand is naive and the other is aware, TypeError is raised if an order comparison is attempted. For equality comparisons, naive instances are never equal to aware instances. If both comparands are aware, and have the same tzinfo attribute, the common tzinfo attribute is ignored and the base datetimes are compared. If both comparands are aware and have different tzinfo attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from self.utcoffset())."
The variable names should be past and past_but_a_little_after. Technically, present is also in the past when the comparison past < present is made.
|
118

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

2 Comments

just do... import time before above code :)
with all the time aware issues in python, this answer ROCKS!
51

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.

3 Comments

The last example doesn't work. Adding up two dates doesn't make sense anyways.
@Joooeey too many pending edits so I can't fix it, but he means -, not +.
Now that there's a - instead of a +, the expression is the same as the second expression. So the result would be the same too. Daniel Nill, can you please edit?
5

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

Comments

5

With python as the easiest language available it is pretty easy to compare dates in python the python operators <, > and == fit wonderfully with datetime objects. each of them has their own meaning in python:

  • < means the date is earlier than the first
  • > means the date comes later
  • == means the date is same as the first So, for your case:
import datetime date = datetime.datetime(2000, 1, 1) # Replace with whatever you want now = datetime.datetime.now() # You can even find the current date and time using this expression if date < now: print('past') elif date > now: print('future') else: print('present') # This would print "past" 

1 Comment

Other answers posted already cover the case. Better to look for new questions to contribute to the site.
0
import datetime from dateutil.parser import parse # Read in the list of holiday dates as a generator expression with open('holiday.txt') as f: holiday_dates = (parse(line.strip()) for line in f) # Get the current date today = datetime.datetime.now().date() # Check if the current date is past the last holiday date if today > max(holiday_dates).date(): # Send email to admin print("Please update the holiday.txt file") 

We use a generator expression to read in the holiday dates, which returns a generator object that yields the parsed dates one at a time. We then use the max() function to find and compare the latest holiday date with the current date. We also use the date() method to convert the datetime objects to date objects, which allows us to compare them directly with the today variable.

Note that the dateutil.parser.parse() method can handle a wider variety of date formats than datetime.datetime.strptime(), so you may not need to specify a format string for the dates in your file. However, if you know the exact format of the dates in your file, you can still use strptime() to parse them if you prefer.

Comments

-2

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

2 Comments

Doesn't answer the question.
How did you ever get this code to run? amount = fine, fine has not been defined.. This code won't run because of that..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.