0

I have the following code:

nowtime = datetime.datetime.now()
newTime = time.strptime(myTimestring, '%Y-%m-%d %H:%M:%S')

if(newTime > nowTime):
  #do some stuff

Of course, my comparison fails with a TypeError, "can't compare datetime.datetime to tuple.". Note that I am using an older version of Python that doesn't have datetime.strptime(). How can I get this comparison to work?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kittyhawk
  • 688
  • 2
  • 11
  • 25
  • Did you look at http://stackoverflow.com/questions/1697815/how-do-you-convert-a-python-time-struct-time-object-into-a-datetime-object ? – milancurcic Jun 25 '13 at 14:53
  • Please state the exact (old) python version so we can browse the docs and seek a proper remedy. – vonPetrushev Jun 25 '13 at 14:54

1 Answers1

1

From the datetime.datetime.strptime() documentation:

This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])).

For older Python versions (e.g. 2.3 or 2.4), use just that:

import datetime
import time

datetime.datetime(*(time.strptime(myTimestring, '%Y-%m-%d %H:%M:%S')[:6]))

Demo:

>>> import datetime
>>> import time
>>> myTimestring = '2013-01-01 12:42:23'
>>> datetime.datetime(*(time.strptime(myTimestring, '%Y-%m-%d %H:%M:%S')[:6]))
datetime.datetime(2013, 1, 1, 12, 42, 23)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343