I have two strings in python.
time_array1='09-JAN-2014 01:19'
time_array2='09-JAN-2014 01:01'
I need to find the time difference and I am doing:
print time_array1
print time_array2
FMT = '%d-%m-%Y %H:%M'
datetime_object1= datetime.datetime.strptime(time_array1, FMT)
print datetime_object1
datetime_object2= datetime.datetime.strptime(time_array2, FMT)
print datetime_object2
diff=datetime_object1 - datetime_object2
print diff
but I am getting the following error:
datetime_object1= datetime.datetime.strptime(time_array1, FMT)
AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
Is there any alternative way through which I can do it. It seems the python library doesn't have strptime
attribute.
The strptime
method was added in python 2.5; if you are using an older version use the following code instead:
import datetime, time
datetime_object1 = datetime.datetime(*time.strptime(time_array1, FMT)[:6])
Your months are abbreviations, use the %b
to parse that instead of %m
.
Demo:
>>> import datetime, time
>>> time_array1='09-JAN-2014 01:19'
>>> time_array2='09-JAN-2014 01:01'
>>> FMT = '%d-%b-%Y %H:%M'
>>> datetime.datetime(*time.strptime(time_array1, FMT)[:6])
datetime.datetime(2014, 1, 9, 1, 19)
>>> datetime.datetime(*time.strptime(time_array2, FMT)[:6])
datetime.datetime(2014, 1, 9, 1, 1)