我試圖將日期時間物件轉換為字串,但它似乎沒有給我所需的輸出。
from datetime import datetime
cur_time = datetime.now()
last_runtime = cur_time.strftime("%Y-%m-%dT%H:%M:%S.%f %Z")
print(last_runtime)
我目前的輸出:
2021-10-13T09:09:27.824592
我想要的輸出:
2021-10-13T09:09:27.825 00:00
uj5u.com熱心網友回復:
您可以.astimezone()在格式字串上使用和小 z:
from datetime import datetime
cur_time = datetime.now()
last_runtime = cur_time.astimezone().strftime("%Y-%m-%dT%H:%M:%S.%f%z")
last_runtime = "{0}:{1}".format(
last_runtime[:-2],
last_runtime[-2:]
)
print(last_runtime)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313654.html
