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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError: 'str' object has no attribute 'strftime'" occurs when we try to call the strftime() method on a string instead of a datetime object. To solve the error, call the method on a datetime object or convert the string to one before calling strftime.

attributeerror str object has no attribute strftime

Here is an example of how the error occurs.

main.py
d = '2022-11-24 09:30:00.000123' # ⛔️ AttributeError: 'str' object has no attribute 'strftime' print(d.strftime('%m,/%d/%Y'))

The issue is that we are calling the date.strftime() on a string instead of a datetime object.

To solve the error, create a datetime object or convert your string to one before calling strftime().

main.py
from datetime import datetime # today = datetime.today() # 👈️ for today's date # 👇️ create datetime object d = datetime(2022, 11, 24, 9, 30, 0) # 👇️ call strftime() method on datetime object print(d.strftime('%m/%d/%Y')) # 👉️ 11/24/2022

If you have a string and need to convert it to a datetime object, use the datetime.strptime method.

main.py
from datetime import datetime d = '2022-11-24 09:30:00.000123' # 👇️ convert string to datetime object datetime_obj = datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') # 👇️ Thursday, 24. November 2022 09:30AM print(datetime_obj.strftime("%A, %d. %B %Y %I:%M%p"))

You can view all the format codes the strftime() and strptime() methods support in this table of the official docs.

Here is an example that prints today's date as YYYY-MM-DD and the current time as hh:mm:ss.

main.py
from datetime import datetime today = datetime.today() print(today) # 👉️ "2022-05-18 13:28:08.718180" todays_date = today.strftime('%Y-%m-%d') print(todays_date) # 👉️ "2022-05-18" current_time = today.strftime('%H:%M:%S') print(current_time) # 👉️ "13:28:08"

If you need to customize the formatting of your date or time, refer to this table for the supported format codes.

The strftime() method is supported by date, datetime and time objects returns a string that represents the date and time, controlled by an explicit format string.

Conversely, the datetime.strptime() method returns a datetime object that corresponds to the provided date string, parsed according to the format.

ValueError is raised if the date string and format can't be parsed by time.strptime().

The strftime() and strptime() methods support the same format codes.

A good way to start debugging is to print(dir(your_object)) and see what attributes a string has.

Here is an example of what printing the attributes of a string looks like.

main.py
my_string = 'hello world' # [ 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', # 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', # 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', # 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', # 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', # 'title', 'translate', 'upper', 'zfill'] print(dir(my_string))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: str object has no attribute error".

Since strftime() is not a method implemented by strings, the error is caused.

Conclusion #

The Python "AttributeError: 'str' object has no attribute 'strftime'" occurs when we try to call the strftime() method on a string instead of a datetime object. To solve the error, call the method on a datetime object or convert the string to one before calling strftime.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.