Python how to format a string time from number time to name time

Question:

I have a string time :

ctime = 'Thu Sep  1 12:25:26 2022'

How can I format it to :

01 Sep 2022 12:25:26

I have tried:

ctime .strftime("%d %m %Y, %H:%M:%S")

But received:

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

Any friend can help ?

Asked By: William

||

Answers:

from datetime import datetime

from datetime import datetime

dt = datetime.strptime(ctime, '%a %b %d %H:%M:%S %Y')
formatted_date = dt.strftime('%d %b %Y %H:%M:%S')
print(formatted_date)  # Output: "01 Sep 2022 12:25:26"

Answered By: Philip Dinu