gpt4 book ai didi

6位毫秒的python datetime

转载 作者:行者123 更新时间:2023-12-02 16:01:11 24 4
gpt4 key购买 nike

我如何以下面的格式打印 6 位毫秒数

>>> import datetime
>>> datetime.datetime.now(tz = datetime.datetime.now().astimezone().tzinfo).isoformat(timespec='milliseconds')
'2022-01-10T18:29:10.698000+05:30'

实际输出:

 '2022-01-10T18:29:10.108+05:30'

期望输出是这样的:

  '2022-01-10T18:29:10.108000+05:30'

最佳答案

使用timespec=microseconds:

>>> dt = datetime.datetime.now(tz = datetime.datetime.now().astimezone().tzinfo).isoformat(timespec='microseconds')
'2022-01-10T14:05:55.742931+01:00'

更新:

如果你想要 0 作为微秒值,你可以这样做:

now = datetime.datetime.now().astimezone()
now = now.replace(microsecond=now.microsecond // 1000 * 1000)
now = now.isoformat(timespec='microseconds')
print(now)

# Output
'2022-01-10T14:17:08.386000+01:00'

关于6位毫秒的python datetime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70652845/

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