如何在Python中获取当前时间(以毫秒为单位)?
#1楼 time.time()可能只给出秒的解析,毫秒的首选方法是datetime 。
from datetime import datetime
dt = datetime.now()
dt.microsecond
#2楼 如果你想在你的代码中使用一个简单的方法返回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
#3楼
def TimestampMillisec64():
return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000)
#4楼 微秒是百万分之一秒,毫秒是千分之一秒钟,这样:
dt.microseconds/1000.0应为dt.microseconds/1000000.0
#5楼
from datetime import datetime
d = datetime.now()
print d.microsecond/1000 + d.second*1000