gpt4 book ai didi

python - 如何在 datetime python 中使用毫秒而不是微秒

转载 作者:行者123 更新时间:2023-12-02 01:51:42 26 4
gpt4 key购买 nike

客户已指定他们使用 DateTime 以 2021-06-22T11:17:09.465Z 格式存储日期,到目前为止我只能以字符串形式获取它日期,因为如果我想保留毫秒数,它会将它们保存为 2021-06-22T11:17:09.465000

是否有任何可能的方法强制 DateTime 使用毫秒而不是微秒?我知道格式中微秒的 %f,但我已经尝试了所有我能想到的方法来减少这 3 位小数,同时保持 DateTime 但没有结果。

最佳答案

我建议使用 timespec 参数,如 python 文档中所述 https://docs.python.org/3/library/datetime.html#datetime.datetime.isoformat :

>>> from datetime import datetime
>>> datetime.now().isoformat(timespec='minutes')
'2002-12-25T00:00'
>>> dt = datetime(2015, 1, 1, 12, 30, 59, 0)
>>> datetime.now().isoformat(timespec='milliseconds')
'2021-12-02T14:03:57.937'

关于python - 如何在 datetime python 中使用毫秒而不是微秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70198931/

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