7

I would like to be able to do greater than and less than against dates. How would I go about doing that? For example:

date1 = "20/06/2013" date2 = "25/06/2013" date3 = "01/07/2013" date4 = "07/07/2013" datelist = [date1, date2, date3] for j in datelist: if j <= date4: print j 

If I run the above, I get date3 back and not date1 or date2. I think I need I need to get the system to realise it's a date and I don't know how to do that. Can someone lend a hand?

Thanks

3
  • 1
    See this or this Commented Jul 5, 2013 at 9:33
  • 1
    judging by the dates on the questions, I don't believe they relate to python 3 Commented Jul 5, 2013 at 9:35
  • Aha! So you already know how to compare dates. Commented Jul 5, 2013 at 9:37

4 Answers 4

10

You can use the datetime module to convert them all to datetime objects. You are comparing strings in your example:

>>> from datetime import datetime >>> date1 = datetime.strptime(date1, "%d/%m/%Y") >>> date2 = datetime.strptime(date2, "%d/%m/%Y") >>> date3 = datetime.strptime(date3, "%d/%m/%Y") >>> date4 = datetime.strptime(date4, "%d/%m/%Y") >>> datelist = [date1, date2, date3] >>> for j in datelist: ... if j <= date4: ... print(j.strftime('%d/%m/%Y')) ... 20/06/2013 25/06/2013 01/07/2013 
Sign up to request clarification or add additional context in comments.

Comments

2

You are comparing strings, not dates. You should use a date-based object-type, such as datetime.

How to compare two dates?

Comments

1

You can use the datetime module:

>>> from datetime import datetime >>> d = datetime.strptime(date4, '%d/%m/%Y') >>> for j in datelist: ... d1 = datetime.strptime(j, '%d/%m/%Y') ... if d1 <= d: ... print j ... 20/06/2013 25/06/2013 01/07/2013 

Comments

0

The problem with your comparison is that a string comparison first compares the first character, followed by the second one, and the third, and so on. You can of course convert the strings to dates, like the other answers suggest, but there is a different solution as well.

In order to compare dates as strings you need to have them in a different format like : 'yyyy-mm-dd'. In this way it first compares the year, than the month and finally the day:

>>> d1 = '2012-10-11' >>> d2 = '2012-10-12' >>> if d2 > d1: ... print('this works!') this works! 

The advantages of this are simplicity (for me at least) and performance because it saves the conversion of strings to dates (and possibly back) while still reliably comparing the dates. In programs I use I compare dates a lot as well. Since I take the dates from files it are always strings to begin with, and because performance is an issue with my program I normally like to compare dates as strings in this way.

Of course this would mean you would have to convert your dates to a different format, but if that is a one time action, it could well be worth the effort.. :)

4 Comments

I doubt that the performance really is an issue. You can do millions of date conversions per second. The fact that you left the hyphens in, despite the ~30% performance hit, is pretty good evidence that this isn't a bottleneck. And if is a bottleneck, the fact that it takes ~3x as long to compare two strings that only differ in the 10th character as to compare two date objects, and that they take more memory (and therefore fewer of them fit into one cache line or VM page), etc. probably matters as well.
@abarnert - Wait, I guess there's a learn for me here. First; why would the hyphens give a 30% performance hit? Do you seriously think that converting all my dates to datetime would increase the speed of my program? In that case I am totally up for a rewrite..
First: How does string comparison work? It compares character by character until there's a mismatch or you reach the end. So, if you need to compare 7 characters before hitting the mismatch, that takes 30% less time than comparing 10 characters. Second, it depends on whether you're comparing the same ones multiple times. There's obviously a performance hit in conversion, but if you compare multiple times, the faster comparisons (it's effectively one int comparison instead of 10 characters) may cancel it out. Will they? Who knows? If you need to optimize, you need to test.
Also, you're missing the most important point here: Is date comparison really a bottleneck in your code? How much of your program's CPU time is spent comparing dates?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.