close

[Solved] AttributeError: ‘str’ object has no attribute ‘strftime’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘str’ object 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: ‘str’ object has no attribute ‘strftime’ Error Occurs?

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

How To Solve AttributeError: ‘str’ object has no attribute ‘strftime’ Error ?

  1. How To Solve AttributeError: 'str' object has no attribute 'strftime' Error ?

    To Solve AttributeError: 'str' object has no attribute 'strftime' Error you should change cr_date(str) to datetime object then you 'll change the date to the specific format:

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

    To Solve AttributeError: 'str' object has no attribute 'strftime' Error you should change cr_date(str) to datetime object then you 'll change the date to the specific format:

Solution 1

you should change cr_date(str) to datetime object then you ‘ll change the date to the specific format:

cr_date = '2013-10-31 18:23:29.000227'
cr_date = datetime.datetime.strptime(cr_date, '%Y-%m-%d %H:%M:%S.%f')
cr_date = cr_date.strftime("%m/%d/%Y")

Solution 2

You should use datetime object, not str.

>>> from datetime import datetime
>>> cr_date = datetime(2013, 10, 31, 18, 23, 29, 227)
>>> cr_date.strftime('%m/%d/%Y')
'10/31/2013'

To get the datetime object from the string, use datetime.datetime.strptime:

>>> datetime.strptime(cr_date, '%Y-%m-%d %H:%M:%S.%f')
datetime.datetime(2013, 10, 31, 18, 23, 29, 227)
>>> datetime.strptime(cr_date, '%Y-%m-%d %H:%M:%S.%f').strftime('%m/%d/%Y')
'10/31/2013'

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