Python datetime无法解释的秒与微秒之间的差异

时间:2013-11-10 09:41:16

标签: python datetime timedelta

这里发生了什么?

>>> a = datetime.datetime.now()
# waiting....
>>> b = datetime.datetime.now()
>>> c = b - a
>>> c.seconds
4
>>> c.microseconds
884704

微秒怎么能比秒数多2倍?我想要微秒的精度(然后我自己把它转换成秒),但这似乎是错误的。

1 个答案:

答案 0 :(得分:6)

884704微秒意味着0.884704秒。

>>> c = datetime.timedelta(seconds=4, microseconds=884704)
>>> c.seconds
4
>>> c.microseconds
884704
>>> print(c)
0:00:04.884704

要获得总秒数,您可以使用total_seconds()

>>> c.total_seconds()
4.884704
相关问题