Home > Net >  Python how to format a string time from number time to name time
Python how to format a string time from number time to name time

Time:01-04

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 ?

CodePudding user response:

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"

  • Related