網站首頁 編程語言 正文
關于日期處理,Python 提供了很多的庫,比如標準庫 datetime、第三方庫 dateutil、arrow 等等。這里介紹一個我個人最喜歡的庫 pendulum,用起來可以說非常的方便,任何對日期的操作它都能滿足。
在使用之前需要先安裝,直接 pip install pendulum 即可。
下面來看一下用法,首先是 datetime, date, time 的創建。
import?pendulum
dt?=?pendulum.datetime(
????2022,?3,?28,?20,?10,?30)
print(dt.__class__)
print(dt)
"""
<class?'pendulum.datetime.DateTime'>
2022-03-28T20:10:30+00:00
"""
#?創建的對象是?DateTime?類型
#?并且帶有時區,默認是?UTC
#?我們可以換一個時區
dt?=?pendulum.datetime(
????2022,?3,?28,?20,?10,?30,?tz="Asia/Shanghai")
print(dt)
"""
2022-03-28T20:10:30+08:00
"""
#?如果不想要時區,那么指定?tz=None
dt?=?pendulum.datetime(
????2022,?3,?28,?20,?10,?30,?tz=None)
print(dt)
"""
2022-03-28T20:10:30
"""
#?然后是?date?的創建
d?=?pendulum.date(2022,?3,?28)
print(d.__class__)
print(d)
"""
<class?'pendulum.date.Date'>
2022-03-28
"""
#?time?的創建
t?=?pendulum.time(20,?10,?30)
print(t.__class__)
print(t)
"""
<class?'pendulum.time.Time'>
20:10:30
"""
如果創建 datetime 時,時區默認是 UTC。如果不想要時區,或者希望時區是本地時區,那么 pendulum 還專門提供了兩個方法。
import?pendulum
#?創建?datetime?時設置為本地時區
#?還是調用了?pendulum.datetime?函數
#?但是?tz?被設置成了?pendulum.local_timezone()
dt?=?pendulum.local(
????2022,?3,?28,?20,?10,?30)
print(dt)
"""
2022-03-28T20:10:30+08:00
"""
print(pendulum.local_timezone())
"""
Timezone('Asia/Shanghai')
"""
#?創建?datetime?時不設置時區
#?內部也是調用了?pendulum.datetime?函數
#?但是?tz?為?None
dt?=?pendulum.naive(2022,?3,?28,?20,?10,?30)
print(dt)
"""
2022-03-28T20:10:30
"""
然后 pendulum 還提供了幾個方法,比如創建當前的 datetime,date 等等。
import?pendulum
#?創建當前的?datetime
#?默認是本地時區,但時區可以指定
dt?=?pendulum.now()
print(dt)
"""
2022-05-29T20:40:49.632182+08:00
"""
#?創建當前的?date,但返回的仍是?datetime
#?只不過時分秒均為?0,同樣可以指定時區
dt?=?pendulum.today()
print(dt)
"""
2022-05-29T00:00:00+08:00
"""
#?獲取明天對應的?date
#?返回的是?datetime,時分秒為?0
#?時區可以指定,默認是本地時區
dt?=?pendulum.tomorrow()
print(dt)
"""
2022-05-30T00:00:00+08:00
"""
#?獲取昨天對應的?date
dt?=?pendulum.yesterday()
print(dt)
"""
2022-05-28T00:00:00+08:00
"""
我們還可以根據時間戳或者字符串來創建:
import?pendulum
#?根據時間戳創建
dt1?=?pendulum.from_timestamp(1653828466)
dt2?=?pendulum.from_timestamp(1653828466,
??????????????????????????????tz=pendulum.local_timezone())
print(dt1)
print(dt2)
"""
2022-05-29T12:47:46+00:00
2022-05-29T20:47:46+08:00
"""
#?根據字符串創建
dt1?=?pendulum.parse("2020-05-03?12:11:33")
dt2?=?pendulum.parse("2020-05-03?12:11:33",
?????????????????????tz=pendulum.local_timezone())
print(dt1)
print(dt2)
"""
2020-05-03T12:11:33+00:00
2020-05-03T12:11:33+08:00
"""
datetime、date、time 的創建我們說完了,然后再來看看它們支持的操作,這也是最核心的部分。
datetime 相關操作
操作非常多,我們逐一介紹。
import?pendulum
dt?=?pendulum.local(
????2022,?3,?28,?20,?10,?30)
#?獲取?date?部分和?time?部分
print(dt.date())
print(dt.time())
"""
2022-03-28
20:10:30
"""
#?替換掉?dt?的某部分,返回新的?datetime
#?年月日時分秒、以及時區都可以替換
print(dt.replace(year=9999))
"""
9999-03-28T20:10:30+08:00
"""
#?轉成時間戳
print(dt.timestamp())
"""
1648469430.0
"""
#?返回年、月、日、時、分、秒、時區
print(dt.year,?dt.month,?dt.day)
print(dt.hour,?dt.minute,?dt.second)
print(dt.tz)
"""
2022?3?28
20?10?30
Timezone('Asia/Shanghai')
"""
然后是生成字符串,pendulum.DateTime 對象可以轉成各種格式的日期字符串。
import?pendulum
dt?=?pendulum.local(
????2022,?3,?28,?20,?10,?30)
#?下面四個最為常用
print("datetime:",?dt.to_datetime_string())
print("date:",?dt.to_date_string())
print("time:",?dt.to_time_string())
print("iso8601:",?dt.to_iso8601_string())
"""
datetime:?2022-03-28?20:10:30
date:?2022-03-28
time:?20:10:30
iso8601:?2022-03-28T20:10:30+08:00
"""
#?當然還支持很多其它格式,不過用的不多
#?隨便挑幾個吧
print("atom:",?dt.to_atom_string())
print("rss:",?dt.to_rss_string())
print("w3c:",?dt.to_w3c_string())
print("cookie:",?dt.to_cookie_string())
print("rfc822:",?dt.to_rfc822_string())
"""
atom:?2022-03-28T20:10:30+08:00
rss:?Mon,?28?Mar?2022?20:10:30?+0800
w3c:?2022-03-28T20:10:30+08:00
rfc822:?Mon,?28?Mar?22?20:10:30?+0800
"""
我們有時也需要判斷當前日期是星期幾、在當前這一年是第幾天等等,pendulum 也已經幫我們封裝好了。
import?pendulum
dt?=?pendulum.local(
????2022,?3,?28,?20,?10,?30)
#?返回星期幾
#?注意:星期一到星期天分別對應 1 到 7
print(dt.isoweekday())??#?1
#?返回一年當中的第幾天
#?范圍是?1?到?366
print(dt.day_of_year)??#?87
#?返回一個月當中的第幾天
print(dt.days_in_month)??#?31
#?返回一個月當中的第幾周
print(dt.week_of_month)??#?5
#?返回一年當中的第幾周
print(dt.week_of_year)??#?13
#?是否是閏年
print(dt.is_leap_year())??#?False
最后就是日期的運算,這是 pendulum 最為強大的地方,至于為什么強大,我們演示一下就知道了。
import?pendulum
dt?=?pendulum.local(
????2022,?3,?30,?20,?10,?30)
#?返回下一個月的今天
print(dt.add(months=1))
"""
2022-04-30T20:10:30+08:00
"""
#?返回上一個月的今天
#?但是上一個月是?2?月,并且是平年
#?所以最多?28?天
print(dt.add(months=-1))
"""
2022-02-28T20:10:30+08:00
"""
#?我們看到處理的非常完美
#?該方法的原型如下,年月日時分秒都是支持的,當然還有星期也支持
"""
def?add(
????self,
????years=0,
????months=0,
????weeks=0,
????days=0,
????hours=0,
????minutes=0,
????seconds=0,
????microseconds=0,
):
"""
像 Python 的內置模塊 datetime 在將日期相加的時候,最多支持到天,我們無法計算下一周、下一個月、下一年的日期。而 pendulum 則可以很方便地處理,這也是我最喜歡的一點。
當然啦,add 里面的值為正,相當于日期往后退;值為負,相當于日期往前推。
然后是兩個日期還可以做減法:
import?pendulum
dt1?=?pendulum.local(
????2021,?1,?20,?11,?22,?33)
dt2?=?pendulum.local(
????2022,?3,?30,?20,?10,?30)
period =?dt2?-?dt1
#?返回的是?Period?對象
#?相當于?datetime?模塊里面的?timedelta
print(period.__class__)
"""
<class?'pendulum.period.Period'>
"""
#?但是功能方面,Period?要強大很多
#?兩者差了多少年
print(period.in_years())??#?1
#?兩者差了多少個月
print(period.in_months())??#?14
#?兩者差了多少個星期
print(period.in_weeks())??#?62
#?兩者差了多少天
print(period.in_days())??#?434
#?兩者差了多少個小時
print(period.in_hours())??#?10424
#?兩者差了多少分鐘
print(period.in_minutes())??#?625487
#?兩者差了多少秒
print(period.in_seconds())??#?37529277
功能非常強大,Python 的 datetime 模塊里面的 timedelta 最多只能計算兩個日期差了多少天,而這里年月日時分秒均可。
原文鏈接:https://mp.weixin.qq.com/s/O2po6H6tR4KXFQzVZwmQeA
相關推薦
- 2022-09-06 Go結構體SliceHeader及StringHeader作用詳解_Golang
- 2023-02-15 Python進行ffmpeg推流和拉流rtsp、rtmp實例詳解_python
- 2022-10-31 Android數據緩存框架內置ORM功能使用教程_Android
- 2022-11-08 PostgreSQL查看帶有綁定變量SQL的通用方法詳解_PostgreSQL
- 2022-01-21 編程實現打印楊輝三角(要求使用一維數組處理)
- 2022-09-01 C++中的Z字形變換問題_C 語言
- 2023-10-16 el-radio單選框,取消選中
- 2022-11-23 GoLang?channel底層代碼實現詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支