import time#調出time模塊python
一、time.time() 時間戳(****) ——>總秒數(值爲格林威治時間1970年1月1日零時零分零秒或北京時間八點零分零秒至如今的總秒數)code
通常用於計算時間時使用對象
print(time.time())#打印出時間戳1571834092.5023854
二、time.ctime() --》獲取當前具備格式的時間ci
print(time.ctime())#Wed Oct 23 20:32:37 2019
三、time.gmtime() --》獲取具備時間對象的時間,對應的時間爲格林威治時間,比北京小時晚8小時字符串
print(time.gmtime())#time.struct_time(tm_year=2019, tm_mon=10, tm_mday=23, tm_hour=12, tm_min=35, tm_sec=58, tm_wday=2, tm_yday=296, tm_isdst=0)
四、time.strftime() --》轉換爲具備時間對象的時間(****)it
''' %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. ''' # 獲取年月日 print(time.strftime('%Y/%m/%d')) # 2019/11/16 # 獲取年月日時分秒 print(time.strftime('%Y/%m/%d %H:%M:%S')) # 2019/11/16 14:38:52 #可簡寫成 print(time.strftime("%Y/%m/%d %X")) # 2019/11/16 14:38:52
#獲取時間對象轉換的格式化時間 res = time.localtime() time.sleep(10) print(time.strftime('%Y-%m-%d %X', res)) # 2019-11-16 14:59:18 #獲取當前時間對象的格式化 print(time.strftime('%Y-%m-%d %X', time.localtime())) # 2019-11-16 14:59:28
五、time.localtime() --->獲取格式化時間對象(struct_time):返回的是一個元組, 元組中有9個值:年、月、日、時、分、秒、一週中第幾天,一年中的第幾天,夏令時class
print(time.localtime()) # time.struct_time(tm_year=2019, tm_mon=11, tm_mday=16, tm_hour=14, tm_min=45, tm_sec=40, tm_wday=5, tm_yday=320, tm_isdst=0) time_local = time.localtime() # 獲取時間對象中的年 print(time_local.tm_year) # 2019 # 獲取時間對象中的月 print(time_local.tm_mon) # 11
六、time.strptime() --》將字符串格式化的時間轉換爲時間對象(***)import
# 將格式化時間轉換成時間對象 print(time.strptime('2019-11-11', '%Y-%m-%d')) # time.struct_time(tm_year=2019, tm_mon=11, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=315, tm_isdst=-1)
time.sleep()程序
time.perf_counter()--》類型於time.time()計時是CPU級別的精確度比較高im