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

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

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:

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

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

But received:

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

Any friend can help ?

>Solution :

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