1. 程式人生 > >python中time模塊獲取時間的使用

python中time模塊獲取時間的使用

轉化 毫秒 3.0 精確 ftime 時間戳 import 格式 local

import time()

·獲取本地時間:

time.time() #本地時間的時間戳格式。

1514273633.0474658

  eg: int(time.time()) , int(time.time()*1000) #本地時間的時間戳格式的秒級和毫秒級別

time.localtime() #本地時間的元組形式。

time.struct_time(tm_year=2017, tm_mon=12, tm_mday=26, tm_hour=15, tm_min=33, tm_sec=53, tm_wday=1, tm_yday=360, tm_isdst=0)

  eg:time.localtime()[0:6] #一般取到前6位精確到秒就足夠

·元組轉化為時間戳:

time.mktime(time.localtime())

1514273772.0

·元組轉化為以字符串表示的某種形式

time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()[0:6])

2017-12-26 15:36:12

·將字符串解析成元組

time.strptime("2017-12-26 14:35:17","%Y-%m-%d %H:%M:%S")

time.struct_time(tm_year=2017, tm_mon=12, tm_mday=26, tm_hour=14, tm_min=35, tm_sec=17, tm_wday=1, tm_yday=360, tm_isdst=-1)

python中time模塊獲取時間的使用