我的輸入程式中有這種格式:“2022-03-07T09:31:49.06251Z”,我需要更改為“YYYYMMDD HH:MM:SS”
請注意,時區是相同的。
樣本:
2022-01-26T14:57:33.2400054Z => 20220126 14:57:33
我發現任何帶有date.fromisoformat和/或.isoformat()但不是自定義格式的代碼。
編輯:
我試試這個:datetime.strptime("2022-03-08T09:57:43.7227461Z", "%Y-%m-%dT%H:%M:%S.%fZ")但我有這個錯誤:
[ERROR] ValueError: time data '2022-03-08T09:57:43.7227461Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
uj5u.com熱心網友回復:
您可以使用名為 dateutil 的庫。
from dateutil import parser
parsed_date = parser.parse('2022-03-07T09:31:49.06251Z')
print(parsed_date)
>>> datetime.datetime(2022, 3, 7, 9, 31, 49, 62510, tzinfo=tzutc())
print(parsed_date.strftime("%Y%m%d %H:%M:%S")
>>> '20220307 09:31:49'
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441132.html
標籤:python-3.x 日期
下一篇:來自2個日期物件的子字串日期
