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
JavaScript
x
7
1
from datetime import datetime
2
linux_date="Fri Feb 25 16:07:17 UTC 2022"
3
arr=linux_date.split(' ')
4
py_str=f"{arr[1]+' '+arr[2]+' '+arr[5]+' '+arr[3]}"
5
py_str=(datetime.strptime(py_str, '%b %d %Y %H:%M:%S')).strftime("%Y-%m-%d %H:%M:%S")
6
py_str
7
Output -> ‘2022-02-25 16:07:17’
Advertisement
Answer
JavaScript
1
6
1
linux_date = 'Fri Feb 25 16:07:17 UTC 2022'
2
3
import datetime
4
new_date = datetime.datetime.strptime(linux_date, '%a %b %d %H:%M:%S %Z %Y')
5
print(new_date)
6
Output
2022-02-25 16:07:17