Works for me (Python 2.6):
>>> import datetime
>>> string1 = '2010-01-01 18:48:14.631829'
>>> string2 = '2010-01-09 13:04:39.540268'
>>> time1 = datetime.datetime.strptime(string1, '%Y-%m-%d %H:%M:%S.%f')
>>> time2 = datetime.datetime.strptime(string2, '%Y-%m-%d %H:%M:%S.%f')
>>> time2 - time1
datetime.timedelta(7, 65784, 908439)
those. between the two dates there are 7 days, 65784 seconds and 908439 microseconds. For information about the object, timedeltasee datetime docs .
Edit: Try the following if you cannot use the directive %f:
>>> time1 = datetime.datetime.strptime(string1.split('.')[0], '%Y-%m-%d %H:%M:%S')
>>> time2 = datetime.datetime.strptime(string2.split('.')[0], '%Y-%m-%d %H:%M:%S')
>>> time2 - time1
datetime.timedelta(7, 65785)
source
share