0

I have a string:

t = "2017-01-05T14:23:33.986-0500"

I need to convert it to a Python date.

I used:

t_obj = datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%f')

I think it is in YYYY-MM-DD with time as HH:MM:SS.sss , but am not able to figure out what -0500 could be...could it be subtracting -5 hrs for UTC?

Any help would be greatly appreciated

EdChum
  • 376,765
  • 198
  • 813
  • 562
Aparna
  • 835
  • 2
  • 23
  • 47
  • t printed out to 2017-01-09T14:01:37.347-0500 and when I tried t_obj = datetime.datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%f%z') ...I get a bad directive error as follows ....Traceback (most recent call last): File "main.py", line 11, in t_obj = datetime.datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%f%z') File "/usr/lib64/python2.7/_strptime.py", line 317, in _strptime (bad_directive, format)) ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%f%z' – Aparna Jan 10 '17 at 12:25
  • if I try t= "2017-01-05T14:23:33.986-0500" dt.datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%f%z')..then I get the following error....Traceback (most recent call last): File "main.py", line 9, in dt.datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%f%z') NameError: name 'dt' is not defined.... – Aparna Jan 10 '17 at 12:32
  • That works for me but I'm running python 3.4, it should work as you seem to be running python 2.7, are you able to upgrade? – EdChum Jan 10 '17 at 13:06
  • No, I cannot upgrade....if I try the following code.............import datetime as dt dt=dt.datetime.strptime("2017-01-05T14:23:33.986", "%Y-%m-%dT%H:%M:%S.%f") print dt.........................it works, but if I try import datetime as dt dt=dt.datetime.strptime("2017-01-05T14:23:33.986-0500", "%Y-%m-%dT%H:%M:%S.%f%z") print dt....it gives the z bad directive error...... – Aparna Jan 10 '17 at 13:52
  • It should work on python 2.7 unless there is some bug in datetime module for python 2.7 – EdChum Jan 10 '17 at 13:53
  • Actually this looks like a bug in 2.7 http://stackoverflow.com/questions/1101508/how-to-parse-dates-with-0400-timezone-string-in-python can you at this post: http://stackoverflow.com/questions/1101508/how-to-parse-dates-with-0400-timezone-string-in-python in particular this answer: http://stackoverflow.com/questions/26165659/python-timezone-z-directive-for-datetime-strptime-not-available/38992623#38992623 – EdChum Jan 10 '17 at 13:56

2 Answers2

4

%z is what you need it's the timezone offset from UTC, see the docs

%z - UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (empty), +0000, -0400, +1030

In [89]:
t= "2017-01-05T14:23:33.986-0500"
dt.datetime.strptime(t, '%Y-%m-%dT%H:%M:%S.%f%z')

Out[89]:
datetime.datetime(2017, 1, 5, 14, 23, 33, 986000, tzinfo=datetime.timezone(datetime.timedelta(-1, 68400)))
EdChum
  • 376,765
  • 198
  • 813
  • 562
0

EdChum's answer is definitely correct. However, there is an easier way to go about parsing dates - python-dateutil:

>>> from dateutil.parser import parse
>>> parse("2017-01-05T14:23:33.986-0500")
datetime.datetime(2017, 1, 5, 14, 23, 33, 986000, tzinfo=tzoffset(None, -18000))

The parse() function is very robust and able to handle a wide variety of date formats:

>>> parse("Friday January 6, 2017 11:12 AM")
datetime.datetime(2017, 1, 6, 11, 12)

This is on 3.6.0 with dateutil 2.6.0.

Community
  • 1
  • 1
MattDMo
  • 100,794
  • 21
  • 241
  • 231