close

[Solved] AttributeError: module ‘datetime’ has no attribute ‘strftime’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: module ‘datetime’ has no attribute ‘strftime’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: module ‘datetime’ has no attribute ‘strftime’ Error Occurs?

Today I get the following error AttributeError: module ‘datetime’ has no attribute ‘strftime’ in python.

How To Solve AttributeError: module ‘datetime’ has no attribute ‘strftime’ Error ?

  1. How To Solve AttributeError: module 'datetime' has no attribute 'strftime' Error ?

    To Solve AttributeError: module 'datetime' has no attribute 'strftime' Error working with datetime gets very confusing once you consider that datetime is both the package name and a module name inside datetime.

  2. AttributeError: module 'datetime' has no attribute 'strftime'

    To Solve AttributeError: module 'datetime' has no attribute 'strftime' Error working with datetime gets very confusing once you consider that datetime is both the package name and a module name inside datetime.

Solution 1

working with datetime gets very confusing once you consider that datetime is both the package name and a module name inside datetime.

the datetime package has a lot of different modules, namely:

datetime module handles datetime objects.

date module handles date objects.

time module handles time objects.

timedelta module handles timedelta objects.

In your case, when you said import datetime, what you’re really referring to is the datetime package NOT the datetime module.

strftime is a method of datetime objects (i.e. under the datetime module)

therefore, you have 2 ways to go about this.

If you went with import datetime, then you have to specify the package, the module THEN the method, like so:

import datetime

today = datetime.datetime.now()
today.strftime('%Y-%m-%d')

or the better, more human readable way is to just import the datetime module under the datetime package by doing a from *package* import *module*. Applied to the datetime package, that means: from datetime import datetime like so:

from datetime import datetime

today = datetime.now()
today.strftime('%Y-%m-%d')

OR, my preferred method, is to give the module an alias (or basically a “nickname”), so it doesn’t get confusing (LOL):

from datetime import datetime as dt
from datetime import timedelta as td

today = dt.now() # get date and time today
delta = td(days=3) #initialize delta
date_you_actually_want = today + delta # add the delta days
date_you_actually_want.strftime('%Y-%m-%d') # format it

hope that clears it up for you.

Solution 2

Your error module 'datetime' has no attribute 'strftime' suggests that it’s not a problem with the imports, but with how you’re calling the strftime() method.

strftime() is a method on the datetime class (which confusingly is part of the datetime module), so you need an instantiated datetime object to call it on. For example:

# import datetime class from the datetime module:
from datetime import datetime

# instantiate a new datetime object:
a = datetime(12, 10, 30, 11, 23, 45)

# call the strftime() method on the object:
print(a.strftime("%H:%M:%S %Z"))
# 11:23:45

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read