新聞中心
Python3 日期和時間
Python 程序能用很多方式處理日期和時間,轉(zhuǎn)換日期格式是一個常見的功能。

創(chuàng)新互聯(lián)公司主營岷縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,岷縣h5小程序設(shè)計搭建,岷縣網(wǎng)站營銷推廣歡迎岷縣等地區(qū)企業(yè)咨詢
Python 提供了一個 time 和 calendar 模塊可以用于格式化日期和時間。
時間間隔是以秒為單位的浮點小數(shù)。
每個時間戳都以自從 1970 年 1 月 1 日午夜(歷元)經(jīng)過了多長時間來表示。
Python 的 time 模塊下有很多函數(shù)可以轉(zhuǎn)換常見日期格式。如函數(shù) time.time() 用于獲取當(dāng)前時間戳, 如下實例:
實例
#!/usr/bin/python3
import
time
# 引入time模塊
ticks
=
time.
time
(
)
print
(
"當(dāng)前時間戳為:"
, ticks
)
以上實例輸出結(jié)果:
當(dāng)前時間戳為: 1459996086.7115328
時間戳單位最適于做日期運算。但是1970年之前的日期就無法以此表示了。太遙遠的日期也不行,UNIX和Windows只支持到2038年。
什么是時間元組?
很多Python函數(shù)用一個元組裝起來的9組數(shù)字處理時間:
| 序號 | 字段 | 值 |
|---|---|---|
| 0 | 4位數(shù)年 | 2008 |
| 1 | 月 | 1 到 12 |
| 2 | 日 | 1到31 |
| 3 | 小時 | 0到23 |
| 4 | 分鐘 | 0到59 |
| 5 | 秒 | 0到61 (60或61 是閏秒) |
| 6 | 一周的第幾日 | 0到6 (0是周一) |
| 7 | 一年的第幾日 | 1到366 (儒略歷) |
| 8 | 夏令時 | -1, 0, 1, -1是決定是否為夏令時的標(biāo)識 |
上述也就是 struct_time 元組。這種結(jié)構(gòu)具有如下屬性:
| 序號 | 屬性 | 值 |
|---|---|---|
| 0 | tm_year | 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(未知),默認(rèn) -1 |
獲取當(dāng)前時間
從返回浮點數(shù)的時間戳方式向時間元組轉(zhuǎn)換,只要將浮點數(shù)傳遞給如localtime之類的函數(shù)。
#!/usr/bin/python3
import time
localtime = time.localtime(time.time())
print ("本地時間為 :", localtime)
以上實例輸出結(jié)果:
本地時間為 : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=28, tm_sec=49, tm_wday=3, tm_yday=98, tm_isdst=0)
獲取格式化的時間
你可以根據(jù)需求選取各種格式,但是最簡單的獲取可讀的時間模式的函數(shù)是asctime():
#!/usr/bin/python3
import time
localtime = time.asctime( time.localtime(time.time()) )
print ("本地時間為 :", localtime)
以上實例輸出結(jié)果:
本地時間為 : Thu Apr 7 10:29:13 2016
格式化日期
我們可以使用 time 模塊的 strftime 方法來格式化日期:
time.strftime(format[, t])
實例
#!/usr/bin/python3
import
time
# 格式化成2016-03-20 11:45:39形式
print
(
time.
strftime
(
"%Y-%m-%d %H:%M:%S"
,
time.
localtime
(
)
)
)
# 格式化成Sat Mar 28 22:24:24 2016形式
print
(
time.
strftime
(
"%a %b %d %H:%M:%S %Y"
,
time.
localtime
(
)
)
)
# 將格式字符串轉(zhuǎn)換為時間戳
a
=
"Sat Mar 28 22:24:24 2016"
print
(
time.
mktime
(
time.
strptime
(a
,
"%a %b %d %H:%M:%S %Y"
)
)
)
以上實例輸出結(jié)果:
2016-04-07 10:29:46 Thu Apr 07 10:29:46 2016 1459175064.0
python中時間日期格式化符號:
- %y 兩位數(shù)的年份表示(00-99)
- %Y 四位數(shù)的年份表示(000-9999)
- %m 月份(01-12)
- %d 月內(nèi)中的一天(0-31)
- %H 24小時制小時數(shù)(0-23)
- %I 12小時制小時數(shù)(01-12)
- %M 分鐘數(shù)(00=59)
- %S 秒(00-59)
- %a 本地簡化星期名稱
- %A 本地完整星期名稱
- %b 本地簡化的月份名稱
- %B 本地完整的月份名稱
- %c 本地相應(yīng)的日期表示和時間表示
- %j 年內(nèi)的一天(001-366)
- %p 本地A.M.或P.M.的等價符
- %U 一年中的星期數(shù)(00-53)星期天為星期的開始
- %w 星期(0-6),星期天為星期的開始
- %W 一年中的星期數(shù)(00-53)星期一為星期的開始
- %x 本地相應(yīng)的日期表示
- %X 本地相應(yīng)的時間表示
- %Z 當(dāng)前時區(qū)的名稱
- %% %號本身
獲取某月日歷
Calendar 模塊有很廣泛的方法用來處理年歷和月歷,例如打印某月的月歷:
實例
#!/usr/bin/python3
import
calendar
cal
=
calendar.
month
(
2016
,
1
)
print
(
"以下輸出2016年1月份的日歷:"
)
print
(cal
)
以上實例輸出結(jié)果:
以下輸出2016年1月份的日歷:
January 2016
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Time 模塊
Time 模塊包含了以下內(nèi)置函數(shù),既有時間處理的,也有轉(zhuǎn)換時間格式的:
| 序號 | 函數(shù)及描述 | 實例 |
|---|---|---|
| 1 | time.altzone 返回格林威治西部的夏令時地區(qū)的偏移秒數(shù)。如果該地區(qū)在格林威治東部會返回負(fù)值(如西歐,包括英國)。對夏令時啟用地區(qū)才能使用。 |
以下實例展示了 altzone()函數(shù)的使用方法:
>>> import time
>>> print ("time.altzone %d " % time.altzone)
time.altzone -28800
|
| 2 | time.asctime([tupletime]) 接受時間元組并返回一個可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時07分14秒)的24個字符的字符串。 |
以下實例展示了 asctime()函數(shù)的使用方法:
>>> import time
>>> t = time.localtime()
>>> print ("time.asctime(t): %s " % time.asctime(t))
time.asctime(t): Thu Apr 7 10:36:20 2016
|
| 3 | 用以浮點數(shù)計算的秒數(shù)返回當(dāng)前的CPU時間。用來衡量不同程序的耗時,比time.time()更有用。 |
由于該方法依賴操作系統(tǒng),在 Python 3.3 以后不被推薦,而在 3.8 版本中被移除,需使用下列兩個函數(shù)替代。 time.perf_counter() # 返回系統(tǒng)運行時間 time.process_time() # 返回進程運行時間 |
| 4 | time.ctime([secs]) 作用相當(dāng)于asctime(localtime(secs)),未給參數(shù)相當(dāng)于asctime() |
以下實例展示了 ctime()函數(shù)的使用方法:
>>> import time
>>> print ("time.ctime() : %s" % time.ctime())
time.ctime() : Thu Apr 7 10:51:58 2016
|
| 5 | time.gmtime([secs]) 接收時間戳(1970紀(jì)元后經(jīng)過的浮點秒數(shù))并返回格林威治天文時間下的時間元組t。注:t.tm_isdst始終為0 |
以下實例展示了 gmtime()函數(shù)的使用方法:
>>> import time
>>> print ("gmtime :", time.gmtime(1455508609.34375))
gmtime : time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=3, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0)
|
| 6 | time.localtime([secs] 接收時間戳(1970紀(jì)元后經(jīng)過的浮點秒數(shù))并返回當(dāng)?shù)貢r間下的時間元組t(t.tm_isdst可取0或1,取決于當(dāng)?shù)禺?dāng)時是不是夏令時)。 |
以下實例展示了 localtime()函數(shù)的使用方法:
>>> import time
>>> print ("localtime(): ", time.localtime(1455508609.34375))
localtime(): time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=11, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0)
|
| 7 | 接受時間元組并返回時間戳(1970紀(jì)元后經(jīng)過的浮點秒數(shù))。 | |
| 8 | time.sleep(secs) 推遲調(diào)用線程的運行,secs指秒數(shù)。 |
以下實例展示了 sleep()函數(shù)的使用方法:
#!/usr/bin/python3
import time
print ("Start : %s" % time.ctime())
time.sleep( 5 )
print ("End : %s" % time.ctime())
|
| 9 | time.strftime(fmt[,tupletime]) 接收以時間元組,并返回以可讀字符串表示的當(dāng)?shù)貢r間,格式由fmt決定。 |
以下實例展示了 strftime()函數(shù)的使用方法:
>>> import time
>>> print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
2016-04-07 11:18:05
|
| 10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') 根據(jù)fmt的格式把一個時間字符串解析為時間元組。 |
以下實例展示了 strptime()函數(shù)的使用方法:
>>> import time
>>> struct_time = time.strptime("30 Nov 00", "%d %b %y")
>>> print ("返回元組: ", struct_time)
返回元組: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
|
| 11 | time.time( ) 返回當(dāng)前時間的時間戳(1970紀(jì)元后經(jīng)過的浮點秒數(shù))。 |
以下實例展示了 time()函數(shù)的使用方法: >>> import time >>> print(time.time()) 1459999336.1963577 |
| 12 | 根據(jù)環(huán)境變量TZ重新初始化時間相關(guān)設(shè)置。 | |
| 13 | time.perf_counter() 返回計時器的精準(zhǔn)時間(系統(tǒng)的運行時間),包含整個系統(tǒng)的睡眠時間。由于返回值的基準(zhǔn)點是未定義的,所以,只有連續(xù)調(diào)用的結(jié)果之間的差才是有效的。 |
|
| 14 | time.process_time() 返回當(dāng)前進程執(zhí)行 CPU 的時間總和,不包含睡眠時間。由于返回值的基準(zhǔn)點是未定義的,所以,只有連續(xù)調(diào)用的結(jié)果之間的差才是有效的。 |
Time模塊包含了以下2個非常重要的屬性:
| 序號 | 屬性及描述 |
|---|---|
| 1 | time.timezone 屬性time.timezone是當(dāng)?shù)貢r區(qū)(未啟動夏令時)距離格林威治的偏移秒數(shù)(>0,美洲;<=0大部分歐洲,亞洲,非洲)。 |
| 2 | time.tzname 屬性time.tzname包含一對根據(jù)情況的不同而不同的字符串,分別是帶夏令時的本地時區(qū)名稱,和不帶的。 |
日歷(Calendar)模塊
此模塊的函數(shù)都是日歷相關(guān)的,例如打印某月的字符月歷。
星期一是默認(rèn)的每周第一天,星期天是默認(rèn)的最后一天。更改設(shè)置需調(diào)用calendar.setfirstweekday()函數(shù)。模塊包含了以下內(nèi)置函數(shù):
| 序號 | 函數(shù)及描述 |
|---|---|
| 1 | calendar.calendar(year,w=2,l=1,c=6) 返回一個多行字符串格式的 year 年年歷,3 個月一行,間隔距離為 c。 每日寬度間隔為w字符。每行長度為 21* W+18+2* C。l 是每星期行數(shù)。 |
| 2 | calendar.firstweekday( ) 返回當(dāng)前每周起始日期的設(shè)置。默認(rèn)情況下,首次載入 calendar 模塊時返回 0,即星期一。 |
| 3 | calendar.isleap(year) 是閏年返回 True,否則為 False。 >>> import calendar >>> print(calendar.isleap(2000)) True >>> print(calendar.isleap(1900)) False |
| 4 | calendar.leapdays(y1,y2) 返回在Y1,Y2兩年之間的閏年總數(shù)。 |
| 5 | calendar.month(year,month,w=2,l=1) 返回一個多行字符串格式的year年month月日歷,兩行標(biāo)題,一周一行。每日寬度間隔為w字符。每行的長度為7* w+6。l是每星期的行數(shù)。 |
| 6 | calendar.monthcalendar(year,month) 返回一個整數(shù)的單層嵌套列表。每個子列表裝載代表一個星期的整數(shù)。Year年month月外的日期都設(shè)為0;范圍內(nèi)的日子都由該月第幾日表示,從1開始。 |
| 7 | calendar.monthrange(year,month) 返回兩個整數(shù)。第一個是該月的星期幾,第二個是該月有幾天。星期幾是從0(星期一)到 6(星期日)。 >>> import calendar >>> calendar.monthrange(2014, 11) (5, 30) (5, 30)解釋:5 表示 2014 年 11 月份的第一天是周六,30 表示 2014 年 11 月份總共有 30 天。 |
| 8 | calendar.prcal(year, w=0, l=0, c=6, m=3) 相當(dāng)于 print (calendar.calendar(year, w=0, l=0, c=6, m=3))。 |
| 9 | calendar.prmonth(theyear, themonth, w=0, l=0) 相當(dāng)于 print(calendar.month(theyear, themonth, w=0, l=0))。 |
| 10 | calendar.setfirstweekday(weekday) 設(shè)置每周的起始日期碼。0(星期一)到6(星期日)。 |
| 11 | calendar.timegm(tupletime) 和time.gmtime相反:接受一個時間元組形式,返回該時刻的時間戳(1970紀(jì)元后經(jīng)過的浮點秒數(shù))。 |
| 12 | calendar.weekday(year,month,day) 返回給定日期的日期碼。0(星期一)到6(星期日)。月份為 1(一月) 到 12(12月)。 |
其他相關(guān)模塊和函數(shù)
在Python中,其他處理日期和時間的模塊還有:
標(biāo)題名稱:Python3日期和時間
地址分享:http://fisionsoft.com.cn/article/copejci.html


咨詢
建站咨詢
