Wikis Wikis - 1 year ago 137
Python Question

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

I'm using strptime to convert a date string into a

. According to the linked page, formatting like this should work:

>>> # Using datetime.strptime()
>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")

My code is:

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

sDate = "07/27/2012"
(for example). (I understand, from the same page, that
is "Year with century as a decimal number.")

I have tried putting the actual value of sDate into the code:

dtDate = datetime.strptime("07/27/2012","%m/%d/%Y")

but this does not work. The error I get is: AttributeError: 'module' object has no attribute 'strptime'

What am I doing wrong?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download