Search code examples
pythonpython-3.xdatetimestrptime

strptime() does not convert string to datetime object in Python 3.6


I am getting an exception when I run the following code which I can not explain:

import datetime
datetime.datetime.strptime("2018-04-02", format = "%Y-%m-%d")

TypeError: strptime() takes no keyword arguments

Solution

  • Remove the format = keyword, the second argument is positional only:

    >>> import datetime
    >>> datetime.datetime.strptime("2018-04-02", "%Y-%m-%d")
    datetime.datetime(2018, 4, 2, 0, 0)