Python:日期和時間的處理模塊及相關函數
Python 提供 time 模塊和 calendar 模塊用於格式化日期和時間。
一、時間戳
在Python中,時間戳是以秒為單位的浮點小數,它是指格林威治時間自1970年1月1日(00:00:00 GMT)至當前時間的總秒數,也被稱為Unix時間戳(Unix Timestamp)。時間戳唯一地標識某一刻的時間。
例如,調用time模塊下的函數time.time()來獲取當前時間戳:
# 導入time模塊
import time t=time.time() print("當前時間戳為:",t)
運行結果:
二、時間元組
時間元組是一個包含9組數字處理時間的元組,其屬性如下所示:
序號 | 屬性 | 說明 | 值 |
0 | tm_year | 4位數年 | 2008 |
1 | tm_mon | 月 | 1 到 12 |
2 | tm_mday | 日 | 1 到 31 |
3 | tm_hour | 小時 | 0 到 23 |
4 | tm_min | 分鍾 | 0 到 59 |
5 | tm_sec | 秒鍾 | 0 到 61 (60或61是閏秒) |
6 | tm_wday | 一周的第幾日 | 0到6 (0是周一) |
7 | tm_yday | 一年的第幾日 | 1 到 366(儒略歷) |
8 | tm_isdst | 夏令時 | -1, 0, 1, -1是決定是否為夏令時的旗幟 |
可以將時間戳轉換為時間元組:
import time t=time.time() T=time.localtime(t) print("當前時間為 :",T)
運行結果:
三、時間日期格式化符號
python中時間日期格式化符號如下所示:
序號 | 符號 | 說明 |
1 | %y | 兩位數的年份表示(00-99) |
2 | %Y | 四位數的年份表示(000-9999) |
3 | %m | 月份(01-12) |
4 | %d | 月內中的一天(0-31) |
5 | %H | 24小時制小時數(0-23) |
6 | %I | 12小時制小時數(01-12) |
7 | %M | 分鍾數(00=59) |
8 | %S | 秒(00-59) |
9 | %a | 本地簡化星期名稱 |
10 | %A | 本地完整星期名稱 |
11 | %b | 本地簡化的月份名稱 |
12 | %B | 本地完整的月份名稱 |
13 | %c | 本地相應的日期表示和時間表示 |
14 | %j | 年內的一天(001-366) |
15 | %p | 本地A.M.或P.M.的等價符 |
16 | %U | 一年中的星期數(00-53)星期天為星期的開始 |
17 | %w | 星期(0-6),星期天為星期的開始 |
18 | %W | 一年中的星期數(00-53)星期一為星期的開始 |
19 | %x | 本地相應的日期表示 |
20 | %X | 本地相應的時間表示 |
21 | %Z | 當前時區的名稱 |
22 | %% | %號本身 |
四、Time模塊函數
Time 模塊包含以下內置函數:
序號 | 函數 | 說明 |
1 | time.altzone | 返回格林威治西部的夏令時地區的偏移秒數。如果該地區在格林威治東部會返回負值(如西歐,包括英國)。對夏令時啟用地區才能使用。 |
2 | time.asctime([tupletime]) | 接受時間元組並返回一個可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時07分14秒)的24個字符的字符串。 |
3 | time.clock() | 用以浮點數計算的秒數返回當前的CPU時間。用來衡量不同程序的耗時,比time.time()更有用。 |
4 | time.ctime([secs]) | 作用相當於asctime(localtime(secs)),未給參數相當於asctime()。 |
5 | time.gmtime([secs]) | 接收時間戳(1970紀元后經過的浮點秒數)並返回格林威治天文時間下的時間元組t。注:t.tm_isdst始終為0。 |
6 | time.localtime([secs]) | 接收時間戳(1970紀元后經過的浮點秒數)並返回當地時間下的時間元組t(t.tm_isdst可取0或1,取決於當地當時是不是夏令時)。 |
7 | time.mktime(tupletime) | 接受時間元組並返回時間戳(1970紀元后經過的浮點秒數)。 |
8 | time.sleep(secs) | 推遲調用線程的運行,secs指秒數。 |
9 | time.strftime(fmt[,tupletime]) | 接收以時間元組,並返回以可讀字符串表示的當地時間,格式由fmt決定。 |
10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') | 根據fmt的格式把一個時間字符串解析為時間元組。 |
11 | time.time( ) | 返回當前時間的時間戳(1970紀元后經過的浮點秒數)。 |
12 | time.tzset() | 根據環境變量TZ重新初始化時間相關設置。 |
使用asctime()獲取格式化的時間:
import time t=time.time() T=time.localtime(t) Ta=time.asctime(T) print("當前時間為 :",Ta)
運行結果:

將格式化字符串轉換為時間戳:
import time a="Sat Nov 28 23:59:59 2019" b=time.strptime(a,"%a %b %d %H:%M:%S %Y") c=time.mktime(b) print(c)
運行結果:
五、Calendar模塊函數
使用Calendar模塊函數可以進行日歷相關操作,例如打印某月的字符月歷。
默認每周第一天為星期一,默認每周最后一天為星期日。更改日期設置需調用calendar.setfirstweekday()函數。
Calendar模塊包含以下內置函數:
序號 | 函數 | 說明 |
1 | calendar.calendar(year,w=2,l=1,c=6) | 返回一個多行字符串格式的year年年歷,3個月一行,間隔距離為c。 每日寬度間隔為w字符。每行長度為21* W+18+2* C。l是每星期行數。 |
2 | calendar.firstweekday() | 返回當前每周起始日期的設置。默認情況下,首次載入 calendar 模塊時返回 0,即星期一。 |
3 | calendar.isleap(year) | 是閏年返回 True,否則為 False。 |
4 | calendar.leapdays(y1,y2) | 返回在Y1,Y2兩年之間的閏年總數。 |
5 | calendar.month(year,month,w=2,l=1) | 返回一個多行字符串格式的year年month月日歷,兩行標題,一周一行。每日寬度間隔為w字符。每行的長度為7* w+6。l是每星期的行數。 |
6 | calendar.monthcalendar(year,month) | 返回一個整數的單層嵌套列表。每個子列表裝載代表一個星期的整數。Year年month月外的日期都設為0;范圍內的日子都由該月第幾日表示,從1開始。 |
7 | calendar.monthrange(year,month) | 返回兩個整數。第一個是該月的星期幾的日期碼,第二個是該月的日期碼。日從0(星期一)到6(星期日);月從1到12。 |
8 | calendar.prcal(year,w=2,l=1,c=6) | 相當於 print calendar.calendar(year,w=2,l=1,c=6)。 |
9 | calendar.prmonth(year,month,w=2,l=1) | 相當於 print calendar.month(year,month,w=2,l=1) 。 |
10 | calendar.prmonth(year,month,w=2,l=1) | 設置每周的起始日期碼。0(星期一)到6(星期日)。 |
11 | calendar.timegm(tupletime) | 和time.gmtime相反:接受一個時間元組形式,返回該時刻的時間戳(1970紀元后經過的浮點秒數)。 |
12 | calendar.weekday(year,month,day) | 返回給定日期的日期碼。0(星期一)到6(星期日)。月份為 1(一月) 到 12(12月)。 |
打印今年的年歷:
import calendar y=calendar.calendar(2019) print(y)
運行結果:
打印當前月的月歷:
# 導入Calendar模塊
import calendar m=calendar.month(2019, 12) print(m)
運行結果:
用calendar.isleap()判斷某一年是否為閏年:
import calendar a=calendar.isleap(2000) b=calendar.isleap(2019) print(a) print(b)
運行結果:
