1.时间戳
import time ticks = time.time() ctime = time.ctime() gmtime = time.gmtime() print(ticks) #1591109841.5101242 print(ctime) #Tue Jun 2 22:57:21 2020 print(gmtime) #time.struct_time(tm_year=2020, tm_mon=6, tm_mday=2, tm_hour=14, tm_min=57, tm_sec=21, tm_wday=1, tm_yday=154, tm_isdst=0) 2020-06-02 22:57:21
2.格式化处理时间
#strftime str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) print(str) #strptime timeStr = '2018-01-26 12:55:20' timeStr2 = time.strptime(timeStr,"%Y-%m-%d %H:%M:%S") print(timeStr2)
3.计算程序运行时间
#起止时间,单位秒 start_time = time.perf_counter() sleep(3.3) end_time = time.perf_counter()