Search code examples
pythondatetimestrptime

strptime pattern for something like 2015-11-21T15:30:00


I need to parse a datetime object from a give date as a string. The date looks like this: 2015-11-21T15:30:00

However, the following fails: datetime.strptime("%Y-%m-%d'T'%H:%M:%S", "2015-11-21T15:30:00") with the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data "%Y-%m-%d'T'%H:%M:%S" does not match format '2015-11-21T15:30:00'

What am I doing wrong here?


Solution

  • The correct format of strptime is (date_string, format). So you were doing it the other way around. Plus, you dont need the ' of the T:

    datetime.strptime("2015-11-21T15:30:00", "%Y-%m-%dT%H:%M:%S")