如何在Python中獲取當前時間(以毫秒爲單位)? spa
time.time()
可能只給出秒的解析,毫秒的首選方法是datetime
。 code
from datetime import datetime dt = datetime.now() dt.microsecond
若是你想在你的代碼中使用一個簡單的方法返回datetime的毫秒: get
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)
微秒是百萬分之一秒,毫秒是千分之一秒鐘,這樣: io
dt.microseconds/1000.0
應爲dt.microseconds/1000000.0
import
from datetime import datetime d = datetime.now() print d.microsecond/1000 + d.second*1000