import time #获取当前的时间戳 浮点型 floatStamp = time.time() print(floatStamp) #获取当前的时间戳 整型 intStamp = int(time.time()) print(intStamp) #从返回的浮点数的时间戳转成时间元组 localtime = time.localtime() print(localtime) #time.struct_time(tm_year=2018, tm_mon=11, tm_mday=20, tm_hour=23, tm_min=11, tm_sec=57, tm_wday=1, tm_yday=324, tm_isdst=0) #将时间字符串解析成时间元组 tupleTime = time.strptime('2018-11-20 23:40:50','%Y-%m-%d %H:%M:%S') print(tupleTime) # time.struct_time(tm_year=2018, tm_mon=11, tm_mday=20, tm_hour=23, tm_min=40, tm_sec=50, tm_wday=1, tm_yday=324, tm_isdst=-1) #将时间戳转成格式化时间 timeFormat1 = time.asctime(time.localtime(time.time())) print(timeFormat1) # Thu Apr 7 10:05:21 2016 timeFormat2 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) print(timeFormat2) # 2018-11-20 23:23:18 #将格式化时间转成时间戳 formatTime = '2018-11-20 23:23:18' tf = int(time.mktime(time.strptime(formatTime,'%Y-%m-%d %H:%M:%S'))) print(tf) # 1542727398 #时间元组转成时间戳【浮点时间戳】 (tm_year,tm_month,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_isdst) t = (2009, 2, 17, 17, 3, 38, 1, 48, 0) floatStamp = time.mktime(t) # 1234861418.0 print(floatStamp) #推迟线程的运行,单位是秒 time.sleep(10) #休眠10s