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

Simkill
  • 357
  • 1
  • 3
  • 17
  • 1
    See [this](http://stackoverflow.com/questions/8142364/how-to-compare-two-dates) or [this](http://stackoverflow.com/questions/3278999/comparing-dates-in-python) – devnull Jul 05 '13 at 09:33
  • 1
    judging by the dates on the questions, I don't believe they relate to python 3 – Simkill Jul 05 '13 at 09:35
  • Aha! So you already know how to compare dates. – devnull Jul 05 '13 at 09:37

4 Answers4

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
TerryA
  • 58,805
  • 11
  • 114
  • 143
2

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

How to compare two dates?

Community
  • 1
  • 1
Erwin
  • 3,298
  • 2
  • 15
  • 22
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
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
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.. :)

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 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 Jul 05 '13 at 12:21
  • @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.. – kramer65 Jul 08 '13 at 07:27
  • 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. – abarnert Jul 08 '13 at 17:33
  • 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? – abarnert Jul 08 '13 at 17:34