Python標(biāo)準(zhǔn)庫(kù)datetime?date模塊的詳細(xì)介紹
前言
datetime是Python提供的操作日期和時(shí)間的標(biāo)準(zhǔn)庫(kù),主要有datetime.date模塊、datetime.time模塊及datetime.datetime模塊。其中date模塊提供了日期操作相關(guān)的方法;time模塊提供了時(shí)間操作相關(guān)的方法;datetime提供了日期時(shí)間操作的相關(guān)內(nèi)容。本文主要介紹datetime.date模塊中常用函數(shù)的使用詳情。
日期對(duì)象類:
- 日期對(duì)象具有3個(gè)屬性:年份、月份、日
- 日期對(duì)象的創(chuàng)建使用date()定義
- date()可以將傳入的參數(shù)轉(zhuǎn)化為datetime.date類型
- date()函數(shù)按位置傳參必須是:年份、月份、日
- date()函數(shù)的參數(shù)中年份的范圍是1-9999,月份的范圍是1-12,日的范圍是1-31
1、定義
- 先導(dǎo)入datetime庫(kù)再操作
# coding:utf-8 import datetime now_date = datetime.date(2022, 12, 26) print(now_date)
1.2、常見(jiàn)錯(cuò)誤
- 缺少參數(shù)
- 按位置傳參時(shí)參數(shù)值超出范圍
# coding:utf-8 import datetime print(datetime.date(2022, 12, 26)) print(type(datetime.date(2022, 12, 26))) # <class 'datetime.date'> # 下面的代碼報(bào)錯(cuò),缺少參數(shù) # print(datetime.date(2022, 12)) # TypeError: function missing required argument 'day' (pos 3) # 下面的代碼提示值錯(cuò)誤,date()函數(shù)的參數(shù)依次為 年份、月份、日;年份的范圍是1-9999,月份的范圍是1-12,日的范圍是1-31 # print(datetime.date(10001, 12, 12)) # ValueError: year 10001 is out of range # print(datetime.date(2022, 13, 12)) # ValueError: month must be in 1..12 # print(datetime.date(2022, 12, 32)) # ValueError: day is out of range for month # 關(guān)鍵字傳傳參,只要保證年份、月份、天的值都在可用范圍內(nèi),位置不受影響 print(datetime.date(day=15, year=2029, month=12))
2、date類常用的函數(shù)
2.1、獲取當(dāng)期日期
# coding:utf-8 import datetime print(datetime.date.today()) # 2022-07-08 print(type(datetime.date.today())) # <class 'datetime.date'>
2.2、格式化日期
2.2.1、ctime()
- 將一個(gè)datetime.date對(duì)象轉(zhuǎn)換為日期時(shí)間格式的字符串
- ctime()函數(shù)的參數(shù)必須是 datetime.date類型
print(datetime.date.ctime(datetime.date.today())) # Fri Jul 8 00:00:00 2022 print(type(datetime.date.ctime(datetime.date.today()))) # <class 'str'> # ctime()函數(shù)的參數(shù)必須是 datetime.date類型 print(datetime.date.ctime(datetime.date(2022, 12, 26))) # Mon Dec 26 00:00:00 2022
2.2.2、datetime.date對(duì)象
datetime_1 = datetime.date(2022, 12, 26) # 獲取日期對(duì)象的日,返回的值為int類型 print(datetime_1.day, type(datetime_1.day), type(datetime_1)) # 26 <class 'int'> <class 'datetime.date'> # 獲取日期對(duì)象的月,返回的值亦為int類型 print(datetime_1.month, type(datetime_1.month), type(datetime_1)) # 12 <class 'int'> <class 'datetime.date'> # 獲取日期對(duì)象的年,返回的值亦為int類型 print(datetime_1.year, type(datetime_1.year), type(datetime_1)) # 2022 <class 'int'> <class 'datetime.date'>
2.2.3、replace(self, year=None, month=None, day=None)
- 替換datetime.date對(duì)象的值
- replace()函數(shù)具有一個(gè)必傳參數(shù),三個(gè)默認(rèn)參數(shù)
- year是要替換的年度
- month是要替換的月份
- day是要替換的日
datetime_1 = datetime.date(2022, 12, 26) print(datetime.date.replace(datetime_1, year=2019, month=11, day=25)) # 2019-11-25 print(type(datetime.date.replace(datetime_1, year=2019, month=11, day=25))) # <class 'datetime.date'> print(datetime_1) # 必須有一個(gè)日期或者時(shí)間對(duì)象的參數(shù),不傳替換參數(shù)即返回傳入的datetime.date對(duì)象的值 print(datetime.date.replace(datetime_1)) # 2022-12-26 # 只替換年度 print(datetime.date.replace(datetime_1, year=2019)) # 2019-12-26 # 只替換月份 print(datetime.date.replace(datetime_1, month=11)) # 2022-11-26 # 只替換日 print(datetime.date.replace(datetime_1, day=25)) # 2022-12-25
通過(guò)觀察,替換年度、月份、日 都是在2022-12-26(datetime_1變量的初始值)基礎(chǔ)上替換的。所以可以肯定的是該函數(shù)不會(huì)改變?cè)兞康闹?/p>
2.2.4、格式化日期
格式符號(hào) | 符號(hào)的含義 |
%y | 兩位數(shù)的年份表示(00-99) |
%Y | 四位數(shù)的年份表示(000-9999) |
%m | 月份(01-12) |
%d | 月內(nèi)中的一天(0-31) |
%H | 24小時(shí)制小時(shí)數(shù)(0-23) |
%I | 12小時(shí)制小時(shí)數(shù)(01-12) |
%M | 分鐘數(shù)(00-59) |
%S | 秒(00-59) |
%a | 本地簡(jiǎn)化星期名稱 |
%A | 本地完整星期名稱 |
%b | 本地簡(jiǎn)化的月份名稱 |
%B | 本地完整的月份名稱 |
%c | 本地相應(yīng)的日期表示和時(shí)間表示 |
%j | 年內(nèi)的一天(001-366) |
%p | 本地A.M.或P.M.的等價(jià)符 |
%U | 一年中的星期數(shù)(00-53)星期天為星期的開(kāi)始 |
%w | 星期(0-6),星期天為 0,星期一為 1,以此類推。 |
%W | 一年中的星期數(shù)(00-53)星期一為星期的開(kāi)始 |
%x | 本地相應(yīng)的日期表示 |
%X | 本地相應(yīng)的時(shí)間表示 |
%Z | 當(dāng)前時(shí)區(qū)的名稱 |
%% | %號(hào)本身 |
datetime_2 = datetime.datetime.today() print(datetime_2, type(datetime_2)) print(datetime.date.strftime(datetime_1, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) print(datetime.date.strftime(datetime_2, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) # 常用的格式: 年、月、日、時(shí)、分、秒 print(datetime.date.strftime(datetime_2, "%Y-%m-%d %H:%M:%S")) # 2022-07-08 18:32:40
2.3、ISO標(biāo)準(zhǔn)格式日期
- 格式:
(4位年-2位月-兩位日)
,如(2022,07,08)
# 返回日期或者時(shí)間對(duì)象的ISO標(biāo)準(zhǔn)日期(4位年-2位月-兩位日) print(datetime.date.isoformat(datetime_1), datetime_1) # 2022-12-26 2022-12-26 print(datetime.date.isoformat(datetime_2), datetime_2) # 2022-07-08 2022-07-08 18:44:36.613676 # 將符合ISO標(biāo)準(zhǔn)格式的日期字符串轉(zhuǎn)換為datetime.date對(duì)象 print(datetime.date.fromisoformat("2022-07-08"), type(datetime.date.fromisoformat("2022-07-08"))) # 不符合ISO標(biāo)準(zhǔn)格式的日期字符串則報(bào)錯(cuò) # print(datetime.date.fromisoformat("2022-7-08")) # ValueError: Invalid isoformat string: '2022-7-08' # print(datetime.date.fromisoformat("2022-07-8")) # ValueError: Invalid isoformat string: '2022-7-08'
2.3.1、獲取符合ISO標(biāo)準(zhǔn)格式的日期字符串的星期幾(1~7)
print(datetime.date.isoweekday(datetime.date(2022, 7, 3))) # 7 星期日 print(datetime.date.isoweekday(datetime.date(2022, 7, 4))) # 1 星期一 print(datetime.date.isoweekday(datetime.date(2022, 7, 8))) # 5 星期五 print(datetime.date.isoweekday(datetime.date(2022, 7, 9))) # 6 星期六
2.3.2、返回日期或者時(shí)間對(duì)象的星期幾(0~6)
# 返回日期或者時(shí)間對(duì)象的星期幾(0~6) print(datetime.date.weekday(datetime.date(2022, 7, 3))) # 6 星期日 print(datetime.date.weekday(datetime.date(2022, 7, 4))) # 0 星期一 print(datetime.date.weekday(datetime.date(2022, 7, 8))) # 4 星期五 print(datetime.date.weekday(datetime.date(2022, 7, 9))) # 5 星期六
2.3.3、根據(jù)時(shí)間戳計(jì)算日期
print(datetime.date.fromtimestamp(2015236523)) # 2033-11-10 print(type(datetime.date.fromtimestamp(2015236523))) # <class 'datetime.date'>
2.3.4、ISO標(biāo)準(zhǔn)格式的時(shí)間元組
- 格式 :
(年份, 周數(shù), 星期數(shù))
,如(2022,26,7)
- 周數(shù):一年中的第幾周,范圍是1~53
- 星期數(shù):星期中的第幾天,周一為第1天,周日為第7天
# 獲取符合ISO標(biāo)準(zhǔn)格式的時(shí)間元組 print(datetime.date.isocalendar(datetime.date(2022, 7, 3))) # datetime.IsoCalendarDate(year=2022, week=26, weekday=7) # 根據(jù)符合ISO標(biāo)準(zhǔn)格式的時(shí)間元組(年份, 周數(shù), 星期數(shù))計(jì)算日期 print(datetime.date.fromisocalendar(2022, 26, 7)) # 2022-07-03
2.4、公元?dú)v格式日期
- 公元1年1月1日為1
print(datetime.date.fromordinal(1), type(datetime.date.fromordinal(1))) # 0001-01-01 <class 'datetime.date'> print(datetime.date.fromordinal(5)) # 0001-01-05 print(datetime.date.fromordinal(738339)) # 2022-07-03 # 返回公元公歷開(kāi)始到現(xiàn)在的天數(shù) print(datetime.date.toordinal(datetime.date(2022, 7, 3))) # 738339
3、其他
# 將 date類型轉(zhuǎn)換為struct_time類型 print(datetime.date.timetuple(datetime.date(2022, 7, 3))) # 日期類型的最大值 print(datetime.date.max) # 日期類型的最小值 print(datetime.date.min)
附錄:完整代碼
# coding:utf-8 import datetime # 日期對(duì)象類(date class) """ * 日期對(duì)象具有3個(gè)屬性:年份、月份、日 * date()可以將指定的三個(gè)參數(shù)轉(zhuǎn)化為datetime.date類的類型 * date()函數(shù)按位置傳參必須是:年份、月份、日的順序 """ print(datetime.date(2022, 12, 26)) print(type(datetime.date(2022, 12, 26))) # <class 'datetime.date'> # 下面的代碼報(bào)錯(cuò),缺少參數(shù) # print(datetime.date(2022, 12)) # TypeError: function missing required argument 'day' (pos 3) # 下面的代碼提示值錯(cuò)誤,date()函數(shù)的參數(shù)依次為 年份、月份、日;年份的范圍是1-9999,月份的范圍是1-12,日的范圍是1-31 # print(datetime.date(10001, 12, 12)) # ValueError: year 10001 is out of range # print(datetime.date(2022, 13, 12)) # ValueError: month must be in 1..12 # print(datetime.date(2022, 12, 32)) # ValueError: day is out of range for month # 關(guān)鍵字傳傳參,只要保證年份、月份、天的值都在可用范圍內(nèi),位置不受影響 print(datetime.date(day=15, year=2029, month=12)) # date類常用的函數(shù) # 獲取當(dāng)期日期 print(datetime.date.today()) # 2022-07-08 print(type(datetime.date.today())) # <class 'datetime.date'> # ctime()是將一個(gè)datetime.date對(duì)象轉(zhuǎn)換為日期時(shí)間格式的字符串 print(datetime.date.ctime(datetime.date.today())) # Fri Jul 8 00:00:00 2022 print(type(datetime.date.ctime(datetime.date.today()))) # <class 'str'> # ctime()函數(shù)的參數(shù)必須是 datetime.date類型 print(datetime.date.ctime(datetime.date(2022, 12, 26))) # Mon Dec 26 00:00:00 2022 # 創(chuàng)建一個(gè) datetime.date對(duì)象 datetime_1 = datetime.date(2022, 12, 26) # 獲取日期對(duì)象的日,返回的值為int類型 print(datetime_1.day, type(datetime_1.day), type(datetime_1)) # 26 <class 'int'> <class 'datetime.date'> # 獲取日期對(duì)象的月,返回的值亦為int類型 print(datetime_1.month, type(datetime_1.month), type(datetime_1)) # 12 <class 'int'> <class 'datetime.date'> # 獲取日期對(duì)象的年,返回的值亦為int類型 print(datetime_1.year, type(datetime_1.year), type(datetime_1)) # 2022 <class 'int'> <class 'datetime.date'> # 替換datetime.date對(duì)象的值 # replace(self, year=None, month=None, day=None) 函數(shù)具有一個(gè)必傳參數(shù),三個(gè)默認(rèn)參數(shù) # year是要替換的年度 # month是要替換的月份 # day是要替換的日 print(datetime.date.replace(datetime_1, year=2019, month=11, day=25)) # 2019-11-25 print(type(datetime.date.replace(datetime_1, year=2019, month=11, day=25))) # <class 'datetime.date'> print(datetime_1) # 必須有一個(gè)日期或者時(shí)間對(duì)象的參數(shù),不傳替換參數(shù)即返回傳入的datetime.date對(duì)象的值 print(datetime.date.replace(datetime_1)) # 2022-12-26 # 只替換年度 print(datetime.date.replace(datetime_1, year=2019)) # 2019-12-26 # 只替換月份 print(datetime.date.replace(datetime_1, month=11)) # 2022-11-26 # 只替換日 print(datetime.date.replace(datetime_1, day=25)) # 2022-12-25 # 通過(guò)觀察,替換年度、月份、日 都是在2022-12-26(datetime_1變量的初始值)基礎(chǔ)上替換的。所以可以肯定的是該函數(shù)不會(huì)改變?cè)醋兞康闹? # 格式化日期 datetime_2 = datetime.datetime.today() print(datetime_2, type(datetime_2)) print(datetime.date.strftime(datetime_1, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) print(datetime.date.strftime(datetime_2, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) # 常用的格式: 年、月、日、時(shí)、分、秒 print(datetime.date.strftime(datetime_2, "%Y-%m-%d %H:%M:%S")) # 2022-07-08 18:32:40 # 返回日期或者時(shí)間對(duì)象的ISO標(biāo)準(zhǔn)日期(4位年-2位月-兩位日) print(datetime.date.isoformat(datetime_1), datetime_1) # 2022-12-26 2022-12-26 print(datetime.date.isoformat(datetime_2), datetime_2) # 2022-07-08 2022-07-08 18:44:36.613676 # 將符合ISO標(biāo)準(zhǔn)格式的日期字符串轉(zhuǎn)換為datetime.date對(duì)象 print(datetime.date.fromisoformat("2022-07-08"), type(datetime.date.fromisoformat("2022-07-08"))) # 不符合ISO標(biāo)準(zhǔn)格式的日期字符串則報(bào)錯(cuò) # print(datetime.date.fromisoformat("2022-7-08")) # ValueError: Invalid isoformat string: '2022-7-08' # print(datetime.date.fromisoformat("2022-07-8")) # ValueError: Invalid isoformat string: '2022-7-08' # 獲取符合ISO標(biāo)準(zhǔn)格式的日期字符串的星期幾(1~7) print(datetime.date.isoweekday(datetime.date(2022, 7, 3))) # 7 星期日 print(datetime.date.isoweekday(datetime.date(2022, 7, 4))) # 1 星期一 print(datetime.date.isoweekday(datetime.date(2022, 7, 8))) # 5 星期五 print(datetime.date.isoweekday(datetime.date(2022, 7, 9))) # 6 星期六 # 返回日期或者時(shí)間對(duì)象的星期幾(0~6) print(datetime.date.weekday(datetime.date(2022, 7, 3))) # 6 星期日 print(datetime.date.weekday(datetime.date(2022, 7, 4))) # 0 星期一 print(datetime.date.weekday(datetime.date(2022, 7, 8))) # 4 星期五 print(datetime.date.weekday(datetime.date(2022, 7, 9))) # 5 星期六 # 根據(jù)時(shí)間戳計(jì)算日期 print(datetime.date.fromtimestamp(2015236523)) # 2033-11-10 print(type(datetime.date.fromtimestamp(2015236523))) # <class 'datetime.date'> # 獲取符合ISO標(biāo)準(zhǔn)格式的時(shí)間元組(年份, 周數(shù), 星期數(shù)) # 周數(shù):一年中的第幾周,范圍是1~53 # 星期數(shù):星期中的第幾天,周一為第1天,周日為第7天 print(datetime.date.isocalendar(datetime.date(2022, 7, 3))) # datetime.IsoCalendarDate(year=2022, week=26, weekday=7) # 根據(jù)符合ISO標(biāo)準(zhǔn)格式的時(shí)間元組(年份, 周數(shù), 星期數(shù))計(jì)算日期 print(datetime.date.fromisocalendar(2022, 26, 7)) # 2022 -07-03 # 將整形按公元?dú)v轉(zhuǎn)換為日期 # * 公元1年1月1日為1 print(datetime.date.fromordinal(1), type(datetime.date.fromordinal(1))) # 0001-01-01 <class 'datetime.date'> print(datetime.date.fromordinal(5)) # 0001-01-05 print(datetime.date.fromordinal(738339)) # 2022-07-03 # 返回公元公歷開(kāi)始到現(xiàn)在的天數(shù) print(datetime.date.toordinal(datetime.date(2022, 7, 3))) # 738339 # 將 date類型轉(zhuǎn)換為struct_time類型 print(datetime.date.timetuple(datetime.date(2022, 7, 3))) # 日期類型的最大值 print(datetime.date.max) # 日期類型的最小值 print(datetime.date.min)
到此這篇關(guān)于Python標(biāo)準(zhǔn)庫(kù)datetime date模塊的詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Python date模塊 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python標(biāo)準(zhǔn)庫(kù) datetime的astimezone設(shè)置時(shí)區(qū)遇到的坑及解決
- Python標(biāo)準(zhǔn)庫(kù)time使用方式詳解
- Python標(biāo)準(zhǔn)庫(kù)datetime之datetime模塊用法分析詳解
- 詳解Python常用標(biāo)準(zhǔn)庫(kù)之時(shí)間模塊time和datetime
- Python標(biāo)準(zhǔn)庫(kù)之time庫(kù)的使用教程詳解
- 一篇文章帶你了解python標(biāo)準(zhǔn)庫(kù)--time模塊
- Python 標(biāo)準(zhǔn)庫(kù)time時(shí)間的訪問(wèn)和轉(zhuǎn)換問(wèn)題小結(jié)
相關(guān)文章
python使用pika庫(kù)調(diào)用rabbitmq交換機(jī)模式詳解
這篇文章主要介紹了python使用pika庫(kù)調(diào)用rabbitmq交換機(jī)模式詳解,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08解決windows下Sublime Text 2 運(yùn)行 PyQt 不顯示的方法分享
問(wèn)題描述:PyQt 環(huán)境正常,可以使用 Windows 的 虛擬 DOS 正常運(yùn)行,但在 Sublime Text 2 下使用 Ctrl + B 運(yùn)行后,界面不顯示,但查看任務(wù)管理器,有 python.exe 進(jìn)程。2014-06-06Python編程中的文件讀寫及相關(guān)的文件對(duì)象方法講解
這篇文章主要介紹了Python編程中的文件讀寫及相關(guān)的文件對(duì)象方法講解,其中文件對(duì)象方法部分講到了對(duì)文件內(nèi)容的輸入輸出操作,需要的朋友可以參考下2016-01-01Python實(shí)現(xiàn)Web指紋識(shí)別實(shí)例
這篇文章主要來(lái)帶大家探索Web指紋識(shí)別:了解主流識(shí)別方式,從標(biāo)題到指紋讀取網(wǎng)站信息的簡(jiǎn)單方法,揭秘Web指紋識(shí)別 關(guān)鍵字、哈希和URL的魔力2023-10-10Python的matplotlib繪圖如何修改背景顏色的實(shí)現(xiàn)
這篇文章主要介紹了Python的matplotlib繪圖如何修改背景顏色的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python使用matplotlib庫(kù)生成隨機(jī)漫步圖
這篇文章主要為大家詳細(xì)介紹了使用Python的matplotlib庫(kù)生成隨機(jī)漫步圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08