Convert unicode to datetime proper strptime format

bbrooke picture bbrooke · Jan 16, 2014 · Viewed 42k times · Source

I am trying to convert a unicode object to a datetime object.

I read through the documentation: http://docs.python.org/2/library/time.html#time.strptime

and tried

datetime.strptime(date_posted, '%Y-%m-%dT%H:%M:%SZ') 

but I get the error message ValueError: time data '2014-01-15T01:35:30.314Z' does not match format '%Y-%m-%dT%H:%M:%SZ'

Any feedback on what is the proper format?

I appreciate the time and expertise.

Answer

Velimir Mlaker picture Velimir Mlaker · Jan 16, 2014

You can parse the microseconds:

from datetime import datetime
date_posted = '2014-01-15T01:35:30.314Z'
datetime.strptime(date_posted, '%Y-%m-%dT%H:%M:%S.%fZ')