How we can solve AttributeError: ‘datetime’ module has no attribute ‘strptime’

In this post we will solve AttributeError: ‘datetime’ module has no attribute ‘strptime’

On trying to convert a date string to a datetime object, you may receive the error message:

AttributeError: ‘datetime’ module has no attribute ‘strptime’

This means that the ‘datetime’ module does not have a ‘strptime’ function, which is needed to convert a date string to a datetime object.

To fix this, you can install the ‘datetime’ module using the ‘pip’ installer. This can be done by running the following command in a terminal:

 

pip install datetime

 

I think you import this:

 

import datetime

 

on the head of your code. So you have to do this:

 

datetime.datetime.strptime(date, "%Y-%m-%d")

 

to access the strptime method. Or, you can change the import statement to this:

 

from datetime import datetime

 

and then access it as you are.

 

Those who created the datetime module also named their class name datetime:

 

#module  class    method
datetime.datetime.strptime(date, "%Y-%m-%d")
Share this post

Leave a Reply

Your email address will not be published.