(1)日曆模塊
calendar()
獲取指定年份的日曆字符串
格式:calendar.calendar(年份,w=2,l=1,c=6,m=3); 返回值:字符串;
w表示 2個日期之間的間隔字符長度; l表示 一個周佔用幾個行高度;
c表示2個月份之間的空白間隔; m表示一行顯示幾個月;
import calendar
ren = calendar.calendar(2018,w=2,l=1,c=6,m=3)
print(ren)
效果圖:
month()
獲取指定年月的日曆字符串
格式:calendar.month(年,月,w=2,l=1); 返回值:字符串
import calendar
ren = calendar.month(2018,12,w=2,l=1)
print(ren)
效果圖:
monthcalendar()
獲取一個年月的矩陣列表
格式:calendar.monthcalendar(年,月); 返回值:二級列表;
結果中0表示不是該月的數值,1-31纔是當月信息
import calendar
ren = calendar.monthcalendar(2018,12)
for page in ren:
print(page)
效果圖:
isleap()
檢測指定年份是不是閏年
格式:calendar.isleap(年份); 返回值:布爾值;
import calendar
ren = calendar.isleap(2018)
print(ren)
# 結果: False
red = calendar.isleap(2016)
print(red)
# 結果: True
leapdays()
檢測指定年份之間的閏年個數
格式:calendar.leapdays(開始年份,結束年份); 返回值:整數; 注意:包含開始年份不包含結束年份;
import calendar
ren = calendar.leapdays(1996,2018)
print(ren)
# 結果: 6
# 分別是: 1996,2000,2004,2008,2012,2016年是閏年,共有6個
monthrange()
獲取一個月的周幾開始及當月天數
格式:calendar.monthrange(年,月); 返回值:元組(周幾,天數); 注意:0-6表示週一到周天;
import calendar
ren = calendar.monthrange(2018,12)
print(ren)
# 結果: (5, 31)
weekday()
根據年月日計算周幾
格式:calendar.weekday(年,月,日); 返回值:整型; 注意:0-6表示週一到周天;
import calendar
ren = calendar.weekday(2018,12,31)
print(ren)
# 結果: 0
(2)時間戳
時間戳是一種用於表示時間的方式。從1970年1月1日0時0分0秒0毫秒開始到指定時間的秒數。世間戳也叫做unix時間戳,1970年1月1日成爲unix元年。
作用:是爲了方便時間的統一運算。
1.從1970年1月1日開始計算。
2.能夠使用到2038年的某一天
3.如果使用太遠的未來或者1970年以前的時間可能出現異常
(3)UTC時間
UTC時間又稱之爲世界協調時間。特指格林尼治天文臺所在位置的時間也叫做格林尼治時間。
中國的時區是東八區,和世界協調時間差了8個小時,多個八個小時
(4)夏令時
夏令時就是通過在夏季將時間調快一小時,來提醒大家早睡早起身體好,節省蠟燭!
每天的時鐘變成了25個小時,注意本質還是24個小時
(5)時間元組struct_time
格式:(年,月,日,時,分,秒,周幾,一年中的第幾天,是否是夏令時
import time
ren = time.localtime()
print(ren)
# 結果: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=31, tm_hour=21, tm_min=4, tm_sec=51, tm_wday=0, tm_yday=365, tm_isdst=0)
print(type(ren))
# 類型: <class 'time.struct_time'>
索引 | 名稱 | 內容 | 取值 |
0 | tm_year | 年 | 年份 |
1 | tm_mon | 月 | 1~12 |
2 | tm_mday | 日 | 1~31 |
3 | tm_hour | 時 | 1~23 |
4 | tm_min | 分 | 1~59 |
5 | tm_sec | 秒 | 1~61(60閏秒,61是歷史保留) |
6 | tm_wday | 周幾 | 0~6 |
7 | tm_yday | 一年中的第幾天 | 1~366 |
8 | tm_isdst | 夏令時 | -1, 0, 1, -1是決定是否爲夏令時的旗幟 |
(6)時間模塊的函數
time()
獲取當前本地的時間戳
格式:time.time(); 返回值:浮點數時間戳;
import time
ren = time.time()
print(ren)
# 結果如: 1546263088.7501929
localtime()
格式:time.localtime(); 返回值:本地時間元組
格式:time.localtime(時間戳); 返回值:指定時間戳的本地時間元組
import time
# 獲取時間戳
res = time.time()
print(res)
# 獲取時間元組
ren = time.localtime()
print(ren)
# 把時間戳轉爲時間元組
red = time.localtime(res)
print(red)
效果圖:
asctime()
返回一個正常的可讀的時間字符串
格式:time.asctime(時間元組); 返回值:時間字符串;
import time
ren = time.localtime() # 獲取時間元組
red = time.asctime(ren)
print(red)
# 結果: Mon Dec 31 21:25:48 2018
gmtime()
格式:time.gmtime(); 返回值:當前UTC時間元組;
格式:time.gmtime(時間戳); 返回值:指定時間戳的UTC時間元組;
import time,datetime
ren = time.gmtime() # 當前UTC時間元組
print(ren)
# 結果: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=31, tm_hour=13, tm_min=39, tm_sec=22, tm_wday=0, tm_yday=365, tm_isdst=0)
res = datetime.datetime.strptime('Dec 30, 2018 03:55:12 PM', '%b %d, %Y %I:%M:%S %p').timestamp() # 獲取指定時間的時間戳
red = time.gmtime(res) # 指定時間戳的UTC時間元組
print(red)
# 結果: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=30, tm_hour=7, tm_min=55, tm_sec=12, tm_wday=6, tm_yday=364, tm_isdst=0)
ctime()
格式: time.ctime(); 返回值:時間格式字符串;
格式: time.ctime(時間戳); 返回值:時間格式字符串;
import time,datetime
ren = time.ctime()
print(ren)
# 結果: Mon Dec 31 21:43:43 2018
# 獲取指定時間的時間戳
res = datetime.datetime.strptime('Dec 30, 2018 03:55:12 PM', '%b %d, %Y %I:%M:%S %p').timestamp()
red = time.ctime(res)
print(red)
# 結果: Sun Dec 30 15:55:12 2018
mktime()
使用時間元組製作時間戳
格式:time.mktime(時間元組); 返回值:時間戳;
注意:按照本地時間來進行計算,如果想按照UTC時間計算,則是calendar.timegm()
import time,calendar
res = time.localtime() # 獲取當前時間元組
ren = time.mktime(res)
print(ren)
# 結果如: 1546264020.0
red = calendar.timegm(res)
print(red)
# 結果如: 1546292820
sleep()
程序睡眠,使得程序在此處等待指定的時間
格式:time.sleep(時間秒數); 返回值:無;
strftime()
import time,datetime
# 格式化輸出時間字符串
ren = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(ren)
# 結果如: 2018-12-31 21:51:32