Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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")
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.