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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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 = '2024-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.

# Call the strftime() method on a datetime object instead

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 a `datetime` object d = datetime(2024, 11, 24, 9, 30, 0) # ๐Ÿ‘‡๏ธ call strftime() method on datetime object print(d.strftime('%m/%d/%Y')) # ๐Ÿ‘‰๏ธ 11/24/2024

call strftime method on datetime object

# Converting a string to a datetime object before calling strftime()

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 = '2024-11-24 09:30:00.000123' # ๐Ÿ‘‡๏ธ Convert the string to datetime object datetime_obj = datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') # ๐Ÿ‘‡๏ธ Sunday, 24. November 2024 09:30AM print(datetime_obj.strftime("%A, %d. %B %Y %I:%M%p"))

convert string to datetime object before calling strftime

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

# Printing today's date as YYYY-MM-DD and the time as hh:mm:ss

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) # ๐Ÿ‘‰๏ธ "2023-07-20 16:11:12.995887" todays_date = today.strftime('%Y-%m-%d') print(todays_date) # ๐Ÿ‘‰๏ธ "2023-07-20" current_time = today.strftime('%H:%M:%S') print(current_time) # ๐Ÿ‘‰๏ธ "16:11:12"

printing todays date as yyyy mm dd hh mm ss

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 and 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 = 'bobbyhadz.com' # [ '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 will get the "AttributeError: str object has no attribute error".

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

If the error persists, follow the instructions in my AttributeError: 'str' object has no attribute 'X in Python article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev