gpt4 book ai didi

Python:从 strftime 打印时区

转载 作者:太空狗 更新时间:2023-10-30 00:31:46 25 4
gpt4 key购买 nike

我想打印时区。我使用了 %Z 但它不打印:

import datetime
now = datetime.datetime.now()
print now.strftime("%d-%m-%Y")
print now.strftime("%d-%b-%Y")
print now.strftime("%a,%d-%b-%Y %I:%M:%S %Z") # %Z doesn't work

我可能需要导入 pytz 吗?

最佳答案

对我来说最简单的:

$ python3
>>> import datetime
>>> datetime.datetime.now().astimezone().strftime("%Y-%m-%dT%H:%M:%S %z")
>>> datetime.datetime.now().astimezone().strftime("%Y-%m-%dT%H:%M:%S %Z")
>>> exit()

关于Python:从 strftime 打印时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31299580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com