One - One Code All

Blog Content

python获取10位和13位时间戳

Python   2011-11-07 17:24:32

10时间戳获取方法:

>>> import time
>>> t = time.time()
>>> print t
>>> print int(t)

强制转换是直接去掉小数位。

2、13位时间戳获取方法:

(1)默认情况下python的时间戳是以秒为单位输出的float

>>> import time
>>> time.time()

通过把秒转换毫秒的方法获得13位的时间戳:

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

round()是四舍五入。

(2)

import time

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

>>> current_milli_time()

13位时间 戳转换成时间:
>>> import time
>>> now = int(round(time.time()*1000))
>>> now02 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now/1000))
>>> now02


上一篇:python日期之计算两个日期相隔月数
下一篇:python读取csv文件

The minute you think of giving up, think of the reason why you held on so long.