Python 2.7 how to parse a date in the format 2014-05-01 18: 10: 38-04: 00

I am trying to parse this datetime string, but have not had time yet, how can I get it?

d = '2014-05-01 18:10:38-04:00' datetime.datetime.strptime(d,'%Y-%m-%d %H:%M:%S-%Z') ValueError: time data '2014-05-01 18:10:38-04:00' does not match format '%Y-%m-%d %H:%M:%S%Z' 
+6
source share
2 answers

Have you tried iso8601 lib? first install it: https://pypi.python.org/pypi/iso8601/

Then:

  import iso8601 mydate = '2014-05-01 18:10:38-04:00' iso8601.parse_date(mydate) Out[3]: datetime.datetime(2014, 5, 1, 18, 10, 38, tzinfo=<FixedOffset '-04:00'>) 
+1
source

You can also use the python-dateutil module:

 >>> from dateutil import parser >>> d = '2014-05-01 18:10:38-04:00' >>> parser.parse(d) datetime.datetime(2014, 5, 1, 18, 10, 38, tzinfo=tzoffset(None, -14400)) 

See also:

+1
source

All Articles