Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: descriptor 'strftime' for 'datetime.date' objects doesn't apply to a 'str' object

I bumped into error message in subject. I extracted the code into bare minimum and I have to admit I’m completely puzzled on what is going on here. Python code looks like this:

from datetime import datetime

time_string = '2022-08-23 08:48:02'
time_time = datetime.strptime(time_string, '%Y-%m-%d %H:%M:%S')
print(type(time_time)) # outputs <class 'datetime.datetime'>
time_string_again= datetime.strftime("%Y-%m-%d",time_time) # throws error in question subject
print(time_string_again)

Could you please explain to me what is wrong with above code?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are using strftime in the wrong way. You can check the right usage here: https://www.programiz.com/python-programming/datetime/strftime

basically, strftime is applied to your datetime object:

time_string_again = time_time.strftime("%Y-%m-%d")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading