Python常用標準庫 time

2021-07-27 19:06:06 字數 1454 閱讀 9517

這個time庫提供了各種操作時間值。 方法

描述示例

time.asctime([tuple])

將乙個時間元組轉換成乙個可讀的24個時間字串

>>> time.asctime(time.localtime())

'sat nov 12 01:19:00 2016'

time.ctime(seconds)

字串型別返回當前時間

>>> time.ctime()

'sat nov 12 01:19:32 2016'

time.localtime([seconds])

預設將當前時間轉換成乙個(struct_timetm_year,tm_mon,tm_mday,tm_hour,tm_min,

tm_sec,tm_wday,tm_yday,tm_isdst)

>>> time.localtime()

time.struct_time(tm_year=2016, tm_mon=11, tm_mday=12, tm_hour=1, tm_min=19, tm_sec=56, tm_wday=5, tm_yday=317, tm_isdst=0)

time.mktime(tuple)

將乙個struct_time轉換成時間戳

>>> time.mktime(time.localtime())

1478942416.0

time.sleep(seconds)

延遲執行給定的秒數

>>> time.sleep(1.5)

time.strftime(format[, tuple])

將元組時間轉換成指定格式。[tuple]不指定預設以當前時間

>>> time.strftime('%y-%m-%d %h:%m:%s')

'2016-11-12 01:20:54'

time.time()

返回當前時間時間戳

>>> time.time()

1478942466.45977

strftime(): 指令

描述%a

簡化星期名稱,如sat

%a完整星期名稱,如saturday

%b簡化月份名稱,如nov

%b完整月份名稱,如november

%c當前時區日期和時間%d天

%h24小時制小時數(0-23)

%i12小時制小時數(01-12)

%j365天中第多少天%m月

%m分鐘

%pam或pm,am表示上午,pm表示下午%s秒

%u一年中第幾個星期

%w星期幾

%w一年中第幾個星期

%x本地日期,如'11/12/16'

%x本地時間,如'17:46:20'

%y簡寫年名稱,如16

%y完整年名稱,如2016

%z當前時區名稱(pst:太平洋標準時間)

%%代表乙個%號本身

python標準庫模組 time

標準庫模組 time 時間 import time 1.獲取當前時間戳 從1970年1月1日到現在經過的秒數 print time.time 1571724503.9006603 2.獲取當前時間元組 年,月,日,時,分,秒,星期,一年的第幾天,與夏令時的偏移量 time.struct time t...

Python標準庫模組之time

import time 返回當前時間戳 1970年後經過的浮點秒數 時間戳是計算機世界中的時間 1555579087.1666212 print time.time 時間戳 時間元組 年,月,日,時,分,秒,星期,一年中的第幾天 夏令時 print time.localtime print time...

Python 標準庫大全之 time模組

time.strftime format,t time.sleep seconds 為python自帶標準庫 import time返回當前時間的時間戳 時間戳 timestamp 表示的是從1970年1月1日00 00 00開始按秒計算的偏移量.示例 print f 當前時間戳為 結果 當前時間戳...