【问题标题】:python time(milli seconds) calculationpython时间(毫秒)计算
【发布时间】:2010-12-26 16:46:03
【问题描述】:

如何计算毫秒,从下面的代码。

a = datetime.datetime.now()
b = datetime.datetime.now()
c = b - a

>>> c

>>> c.days
0
>>> c.seconds
4
>>> c.microseconds

【问题讨论】:

标签: python time


【解决方案1】:
milliseconds = (c.days * 24 * 60 * 60 + c.seconds) * 1000 + c.microseconds / 1000.0

【讨论】:

  • 这个答案是错误的!如果时间增量正好是 1 分钟,结果将为 0,不是我们的预期!
【解决方案2】:

或者,自 2.7 以来的新功能:

c.total_seconds()*1000

(https://docs.python.org/2/library/datetime.html)

【讨论】:

    猜你喜欢
    • 2013-05-05
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    相关资源
    最近更新 更多