Getting time difference in python 2.7

I am importing some data from REST API into a list. One of the columns contains only date / time.

Column A format / example: 2015-06-11 07:59:10.000 GMT

I need to be able to check the time difference between two lines, I tried using

datetime.strptime(variable_name, "%Y-%m-%d %H:%M:%S")

      

however i got the following error:

ValueError: unconverted data remains: .000 GMT

      

Is there a way to fix this without changing or removing the offending portion from every entry on my list?

Any help would be greatly appreciated.

+3


source to share


2 answers


try it

datetime.strptime (time , "%Y-%m-%d %H:%M:%S.%f %Z")

      



" % f " is whatever comes after seconds. It will give microseconds and " % Z " will provide the timezone

+4


source


You need to use %f

for decimal and %Z

for timezone:



>>> variable_name='2015-06-11 07:59:10.000 GMT'
>>> datetime.strptime(variable_name, "%Y-%m-%d %H:%M:%S.%f %Z")
datetime.datetime(2015, 6, 11, 7, 59, 10)

      

+4


source







All Articles