Python 時間獲取
要使用到python time模塊
代碼如下:
import time
print time.time()
結果:
1472483797.276373
結果爲浮點型的 時間戳
指格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒起至現在的總毫秒數)
通常10位的時間戳就足夠我們使用了(精確到秒),但是也有需要取13位時間戳(精確到毫秒)的情況,對此只需要採取簡單的操作:
print int(time.time()*1000)
結果爲:
1472484126055
那麼 如何解析呢(注意計算時間爲格林威治時間,而我們爲東八區)
使用localtime()
和格式化輸出strftime()
now = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
print now
輸出結果:
20160829232513’