others - 在Python获取当前时间( 毫秒) ?

如何在python中以毫秒为单位获取当前时间?

时间:


import time
millis = int(round(time.time() * 1000))
print millis

为了重复使用:


import time

current_milli_time = lambda: int(round(time.time() * 1000))

然后:


>>> current_milli_time()
1378761833768


from datetime import datetime
dt = datetime.now()
dt.microsecond

另一个解决方案,可以嵌入到自己的utils.py中的函数


import time as time_ #make sure we don't override time
def millis():
 return int(round(time_.time() * 1000))

如果要在代码中使用datetime返回毫秒的简单方法,请执行以下操作:


from datetime import datetime
from datetime import timedelta

start_time = datetime.now()

# returns the elapsed milliseconds since the start of the program
def millis():
 dt = datetime.now() - start_time
 ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0
 return ms


def TimestampMillisec64():
 return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000) 

微秒是 1/1000000 秒,毫秒是 1/1000 秒所以

dt 。微秒/1000.0 应该是 dt 。微秒/1000000.0

...