gpt4 book ai didi

python - Python 中的时间戳 YYYY-MM-DDThh :mm:ss. sssTZD

转载 作者:太空宇宙 更新时间:2023-11-04 03:12:40 25 4
gpt4 key购买 nike

我正在努力使用 Python 将当前 UTC 时间转换为这种格式:YYYY-MM-DDThh:mm:ss.sssTZD(例如 1997-07-16T19:20:30.000+00:00)

最佳答案

尝试这样的事情。使用 datetimedateutil

In [1]: from dateutil.tz import tzoffset
In [2]: from datetime import datetime
In [3]: now = datetime.now(tzoffset('EDT', +4*60*60))
In [4]: print now.isoformat()
2016-05-28T01:43:14.702253+04:00

要删除毫秒,请使用类似的东西。

now.strftime("%Y-%m-%dT:%H:%M:%S") 

去除毫秒的另一种方法

In [1]: now = datetime.now(tzoffset('EDT', +4*60*60)).replace(microsecond=0)
In [2]: print now.isoformat()
2016-05-28T01:43:14+04:00

关于python - Python 中的时间戳 YYYY-MM-DDThh :mm:ss. sssTZD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37492554/

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