chevron_left
240 points
4 2 2

Attributeerror: 'str' object has no attribute 'strftime'
Photo by pixabay.com

The python library named datetime allows the use of dates and times during programming. On the datetime module, the method datetime.strftime() is useful as it converts datetime into suitable string formats. Therefore, strftime() is useful when working with date and time. You cannot use the method strftime() on strings since doing so results in the error 'AttributeError: 'str' object has no attribute 'strftime'.

Reasons for Attributeerror: 'str' object has no attribute 'strftime'

The AttributeError appears when a method that does not exist gets called by an object. Hence, the 'str' object has no attribute 'strftime' implies that our object string does not have the method strftime(). And as earlier mentioned, the strftime() class belongs to datetime class; its main function is converting date objects into string format for better readability.

Example Code

First, let's take a look at how the error appears to developers in the course of software development:

#import the module datetime
import datetime
#create a variable today_date and assign it to a date
today_date = '2022-09-12'
#change the date in the var to datetime object and display
print(datetime.strptime(today_date,'%Y-%m-%d'))
When we execute the code, we end up with the result:
print(datetime.strptime(today_date,'%Y-%m-%d'))

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

Explanation.

The example above tries to call the method datetime.strptime() inside the datetime module to change the string datetime into an object. However, when we execute it, we end up with the error: AttributeError: module 'datetime' has no attribute. The reason for this error is that we try to use the datetime module with the method strptime(). However, the module does not have a method. Solving the error requires a deep understanding of the fact that the module has got a class named datetime which in turn hosts the method strptime(). As you may have noticed, the module and class have the same name datetime hence confusing especially to new developers.

Solution.

We can go about solving the errors in two ways both pointing to the manner we access the strptime() method:

Method 1: First Import the module followed by the class.

When you want to use the datetime module in your code directly, then the best way is to use the syntax: datetime.datetime.strptime ().

As you may have noticed, we have two datetimes. The first is the module name while the second is a class that holds the method strptime ().

How it solves the Attributeerror: 'str' object has no attribute 'strftime' above

#import the module datetime
import datetime
#create a variable today_date and assign it to a date
today_date = '2022-09-12'
#change the date in the var to datetime object and display
print(datetime.datetime.strptime(today_date,'%Y-%m-%d'))

Output:

2022-09-12 00:00:00

Method 2: First import the class from the module .

The second way is to first import the class, using the syntax: from datetime import datetime. This allows us to go to the class inside the module and hence access all the methods.

How to solve the Attributeerror: 'str' object has no attribute 'strftime' error.

#import the class datetime from the datetime module
from datetime import datetime
#create a variable today_date and assign it to a date
today_date = '2022-09-12'
#change the date in the var to datetime object and display
print(datetime.strptime(today_date,'%Y-%m-%d'))

Output:

2022-09-12 00:00:00

For example, let's have a look at a string representing a date in the order Day/Month/Year. We call the method strftime() on the string.

#import the class datetime from the datetime module
from datetime import datetime
today_date='2022-09-12 00:02:23.000008'
format_date = today_date.strftime(today_date, "%d/%m/%Y")

The code output is:

format_date = today_date.strftime(today_date, "%d/%m/%Y")

AttributeError: 'str' object has no attribute 'strftime'

Explanation:

We meet the error because of calling the method to a string.

More help:

The strptime() method always come in handy to convert the datetime object to a similar format as that of the string. Here is how we solve the error:

from datetime import date, datetime
today_date='2022-09-12 00:02:23.000008'
leo=datetime.strptime(today_date,'%Y-%m-%d %H:%M:%S.%f')
print(leo)

Hence, the final output, which is correct for solving Attributeerror: 'str' object has no attribute 'strftime' is:

2022-09-12 00:02:23.000008

Conclusion

The main takeaway here is that the module datetime does not have the method strptime(). If we use the module with a method directly, we end up with this error. We can solve the error directly by calling the module followed by the class and then ultimately the method. Alternatively, we import the class into the module.

It only supports 16 significant digits, regardless of the precision and scale specified.
If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

AttributeError: 'str' object has no attribute 'remove' sumitthakur11 - Sep 16
TypeError: object of type 'NoneType' has no len() zhteja - Aug 14
Dataframe' object has no attribute 'sort' AAbouelenien - Sep 17
Dataframe' object has no attribute 'ix' AAbouelenien - Sep 15
A bytes-like object is required, not 'str' Ankur Ranpariya - Sep 16
Typeerror a bytes like object is required not str Ankur Ranpariya - Aug 29
TypeError: unsupported operand type(s) for -: 'str' and 'str' Ferdy - Sep 9
TypeError: can only concatenate str (not "nonetype") to str NoirHusky - Sep 7
TypeError: unsupported operand type(s) for +: 'nonetype' and 'str' Ferdy - Sep 5
Object of type 'zip' has no len() Aizhamal Zhetigenova - Jun 1
TypeError: 'int' object is not iterable in Python Ferdy - Sep 30
Typeerror: module object is not callable Ankur Ranpariya - Sep 24
Cannot Unpack Non-Iterable Int Object zhteja - Sep 23
TypeError: a bytes-like object is required, not 'Problem' beekip - Sep 17
[Solved] Typeerror: 'numpy.float64' object is not iterable beekip - Sep 15
TypeError: Expected String or Bytes-Like Object zhteja - Jul 31
TypeError: 'str' object does not support item assignment zhteja - Aug 1
Str' object does not support item assignment zhteja - Jul 30
Typeerror: string indices must be integers Ankur Ranpariya - Sep 27
TypeError: unsupported operand type(s) for -: 'list' and 'list' Ferdy - Sep 9