Python: strptime () not working

I am trying to convert a string to a date. If I read the doc correctly , the following code should work:

import datetime
d = datetime.strptime('January 01, 2015', '%B %d, %Y')

      

Instead, I get the error:

AttributeError: 'module' object has no attribute 'strptime'

      

+3


source to share


1 answer


You need to import datetime

from datetime

:

from datetime import datetime
d = datetime.strptime('January 01, 2015', '%B %d, %Y')

      



Or use:

import datetime
datetime.datetime.strptime('January 01, 2015', '%B %d, %Y')

      

+6


source







All Articles