Last updated: Apr 8, 2024
Reading timeยท3 min
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
.
Here is an example of how the error occurs.
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.
strftime()
method on a datetime
object insteadTo solve the error, create a datetime
object or convert your string to one
before calling strftime()
.
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
datetime
object before calling strftime()
If you have a string and need to convert it to a datetime
object, use the
datetime.strptime()
method.
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"))
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
.
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"
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: