Why is datetime.strptime not working in this simple example? Why is datetime.strptime not working in this simple example? python python

Why is datetime.strptime not working in this simple example?


You should be using datetime.datetime.strptime. Note that very old versions of Python (2.4 and older) don't have datetime.datetime.strptime; use time.strptime in that case.


You are importing the module datetime, which doesn't have a strptime function.

That module does have a datetime object with that method though:

import datetimedtDate = datetime.datetime.strptime(sDate, "%m/%d/%Y")

Alternatively you can import the datetime object from the module:

from datetime import datetimedtDate = datetime.strptime(sDate, "%m/%d/%Y")

Note that the strptime method was added in python 2.5; if you are using an older version use the following code instead:

import datetime, timedtDate = datetime.datetime(*time.strptime(sDate, "%m/%d/%Y")[:6])


Because datetime is the module. The class is datetime.datetime.

import datetimedtDate = datetime.datetime.strptime(sDate,"%m/%d/%Y")