Python Datetime模塊和Calendar模塊用法實(shí)例分析
本文實(shí)例講述了Python Datetime模塊和Calendar模塊用法。分享給大家供大家參考,具體如下:
datetime模塊
1.1 概述
datetime比time高級(jí)了不少,可以理解為datetime基于time進(jìn)行了封裝,提供了更多的實(shí)用的函數(shù),datetime的接口更加的直觀,更容易調(diào)用
1.2 模塊中的類
datetime:同時(shí)有時(shí)間與日期
timedelta:表示時(shí)間間隔,即兩個(gè)時(shí)間點(diǎn)的間隔:主要用于計(jì)算時(shí)間的跨度
tzinfo: 時(shí)區(qū)相關(guān)的信息
date : 只關(guān)注日期
2、獲取系統(tǒng)當(dāng)前時(shí)間
先導(dǎo)入模塊:
import datetime t1 = datetime.datetime.now() print(t1)
輸出:
2018-04-11 19:52:06.180339
3、獲取指定時(shí)間
time2 = datetime.datetime(2018, 3, 28, 21, 59, 7, 95015) print(time2) print(type(time2))
輸出:
2018-03-28 21:59:07.095015
<class 'datetime.datetime'>
4、將時(shí)間轉(zhuǎn)為字符串
time1 = datetime.datetime.now() time3 = time1.strftime("%Y-%m-%d") print(time3)
輸出:
2018-04-11
5、時(shí)間相減,返回一個(gè)時(shí)間間隔的對(duì)象
import datetime import time time1 = datetime.datetime.now() time.sleep(3) time2 = datetime.datetime.now() time3 = time2 -time1 print(time1) print(time2) print(time3) print(type(time3)) #間隔天數(shù) print(time3.days) # 間隔天數(shù)之外的時(shí)間轉(zhuǎn)為秒 print(time3.seconds)
輸出:
2018-04-11 20:06:11.439085
2018-04-11 20:06:14.440052
0:00:03.000967
<class 'datetime.timedelta'>
0
3
calendar模塊
1、calendar模塊有很廣泛的方法用來處理年歷和月歷
導(dǎo)入模塊
import calendar
2、calendar.month(year.month)
返回指定年月的日歷【字符串類型】
print(calendar.month(2018,4)) print(type(calendar.month(2018,4)))
輸出:
April 2018
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<class 'str'>
3、calendar.calendar(year)
返回指定年的日歷【字符串類型】
4、calendar.firstweekday()
返回當(dāng)前每周起始日期的設(shè)置
print(calendar.firstweekday())
輸出:
0
5、calendar.isleap(year)
返回指定的年份是否為閏年,若是返回True,否則返回False
print(calendar.isleap(2016))
輸出:
True
6、calendar.leapdays(year1,year2)
返回[year1,year2)之間閏年的總和。
print(calendar.leapdays(2000,2020))
輸出:
5
7、calendar.monthrange(year,month)
返回一個(gè)元組(參數(shù)一,參數(shù)二)
參數(shù)一:當(dāng)月的天數(shù)
參數(shù)二:當(dāng)月第一天的日期碼[0,6][周一,周日]
print(calendar.monthrange(2018,1)) print(calendar.monthrange(2018,2)) print(calendar.monthrange(2018,3)) print(calendar.monthrange(2018,4))
輸出:
(0, 31)
(3, 28)
(3, 31)
(6, 30)
8、calendar.monthlendar(year,month)
返回指定月份以每一周為元素的一個(gè)二維列表。
print(calendar.monthcalendar(2018,4))
輸出:
[[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]
9、calendar.weekday(year,month,day)
返回指定日期的日期碼。
print(calendar.weekday(2018,4,1))
輸出:
6
9、獲取凌晨零點(diǎn)到23:59的時(shí)間
now = time.time() midnight = now - (now % 86400) + time.timezone pre_midnight = midnight - 86400 now_midnight = midnight - 1 start_time = datetime.datetime.strptime(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(pre_midnight)), "%Y-%m-%d %H:%M:%S") end_time = datetime.datetime.strptime(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now_midnight)), "%Y-%m-%d %H:%M:%S")
PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計(jì)算的在線工具供大家使用:
在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時(shí)間操作技巧總結(jié)》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python利用datetime模塊計(jì)算時(shí)間差
- Python中的time模塊與datetime模塊用法總結(jié)
- python使用datetime模塊計(jì)算各種時(shí)間間隔的方法
- 詳解python時(shí)間模塊中的datetime模塊
- python中datetime模塊中strftime/strptime函數(shù)的使用
- Python中time模塊和datetime模塊的用法示例
- Python中time模塊與datetime模塊在使用中的不同之處
- python?datetime模塊詳解
- Python中datetime模塊參考手冊(cè)
- Python時(shí)間處理模塊time和datetime詳解
相關(guān)文章
baselines示例程序train_cartpole.py的ImportError
這篇文章主要為大家介紹了baselines示例程序train_cartpole.py的ImportError引入錯(cuò)誤詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05深入講解Python函數(shù)中參數(shù)的使用及默認(rèn)參數(shù)的陷阱
這篇文章主要介紹了Python函數(shù)中參數(shù)的使用及默認(rèn)參數(shù)的陷阱,文中將函數(shù)的參數(shù)分為必選參數(shù)、默認(rèn)參數(shù)、可變參數(shù)和關(guān)鍵字參數(shù)來講,要的朋友可以參考下2016-03-03python中l(wèi)ower函數(shù)實(shí)現(xiàn)方法及用法講解
在本篇文章里小編給大家整理的是一篇關(guān)于python中l(wèi)ower函數(shù)實(shí)現(xiàn)方法及用法講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2020-12-12Python實(shí)現(xiàn)多格式文本轉(zhuǎn)為word
在現(xiàn)代工作中,我們常常需要處理不同格式的文件,其中Word文檔是最為常見的一種,本文主要介紹了如何使用Python創(chuàng)建一個(gè)全能的文件處理工具,能夠?qū)⒍喾N格式的文件轉(zhuǎn)換為Word文檔,需要的可以參考下2023-11-11Python簡單過濾字母和數(shù)字的方法小結(jié)
這篇文章主要介紹了Python簡單過濾字母和數(shù)字的方法,涉及Python基于內(nèi)置函數(shù)與正則表達(dá)式進(jìn)行字母和數(shù)字過濾的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01