I'm getting the following format date time strings from a JSON object and i want to convert it to a datetime object in Python, so that i can use it in my code.
'2019-06-01T23:07:02.000+0000'
As you can see the above string contains milliseconds and i couldn't find a formatter in strptime method for it.
datetime.strptime(incOpenTime, '%Y-%m-%dT%H:%M:%S.%fZ') #this works only against microseconds
I can parse the string and supply it to datetime object, but i'm hoping for a direct strptime format.
You would want to change your formatter string to '%Y-%m-%dT%H:%M:%S.%f%z
In [25]: datetime.strptime('2019-06-01T23:07:02.000+0000','%Y-%m-%dT%H:%M:%S.%f%z')
Out[25]: datetime.datetime(2019, 6, 1, 23, 7, 2, tzinfo=datetime.timezone.utc)
Or you can use datetutil.parser for the same, which doesn't need you to provide the format string
'2019-06-01T23:07:02.000+0000'
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-d8d1b2eea17c> in <module>
----> 1 from datetutil import parser
ModuleNotFoundError: No module named 'datetutil'
In [2]: from dateutil import parser
In [4]: parser.parse('2019-06-01T23:07:02.000+0000')
Out[4]: datetime.datetime(2019, 6, 1, 23, 7, 2, tzinfo=tzutc())