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

How to convert Linux date string to Python Datetime

I am trying to convert the Linux date time ‘Fri Feb 25 16:07:17 UTC 2022’ to python datetime but not able to achieve the same. However, I am able to do using below code, but still looking for the right approach:

Input -> Fri Feb 25 16:07:17 UTC 2022

from datetime import datetime
linux_date="Fri Feb 25 16:07:17 UTC 2022"
arr=linux_date.split(' ')
py_str=f"{arr[1]+' '+arr[2]+' '+arr[5]+' '+arr[3]}"
py_str=(datetime.strptime(py_str, '%b %d %Y %H:%M:%S')).strftime("%Y-%m-%d %H:%M:%S")
py_str

Output -> ‘2022-02-25 16:07:17’

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 :

linux_date = 'Fri Feb 25 16:07:17 UTC 2022'

import datetime
new_date = datetime.datetime.strptime(linux_date, '%a %b %d %H:%M:%S %Z %Y')
print(new_date)

Output

2022-02-25 16:07:17

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