PyTime是一个易于使用的Python模块,指在通过字符串操作date/time/datetime。在大多数情况下,PyTime允许你使用非规则的datetime字符串来生成和计算datetime。它还提供了一些简单有用的方法来获取你想要的日期时间。
安装
C:\Users\lifeng01>pip?install?pytime
Collecting?pytime
??Downloading?pytime-0.2.2-py3-none-any.whl?(9.2?kB)
Installing?collected?packages:?pytime
Successfully?installed?pytime-0.2.2
示例使用
from?pytime?import?pytime
#?获取2015.5.17前2年,3个月23周3天2小时
data_time?=?pytime.before('2015.5.17',?'2years?3mon?23week?3d?2hr')
print(f"类型:{type(data_time)},日期:{data_time}")
#?获取2015年5月17日的第二天,23个月3天29分钟
data_time?=?pytime.after('2015.5.17',?'23month3dy29minu')
print(f"类型:{type(data_time)},日期:{data_time}")
#?运行结果
类型:<class?'datetime.datetime'>,日期:2012-09-05?22:00:00
类型:<class?'datetime.datetime'>,日期:2017-04-20?00:29:00
Process?finished?with?exit?code?0
返回的类型全部是datetime.datetime,因为本身这个库中是基于datetime库编写的。
获取天
from?pytime?import?pytime
print(pytime.today())
print(pytime.tomorrow())
print(pytime.yesterday())
print(pytime.last_day())
#?运行结果
2021-10-21
2021-10-22
2021-10-20
2021-10-31
Process?finished?with?exit?code?0
from?pytime?import?pytime
print(pytime.tomorrow("2021.10.15"))
print(pytime.yesterday("2021.10.15"))
print(pytime.last_day(2021,?9))
#?运行结果
2020-10-21
2021-10-16
2021-10-14
2021-09-30
Process?finished?with?exit?code?0
获取周
from?pytime?import?pytime
print(pytime.this_week())
print(pytime.next_week())
print(pytime.last_week())
#?运行结果
(datetime.date(2021,?10,?18),?datetime.date(2021,?10,?25))
(datetime.date(2021,?10,?25),?datetime.date(2021,?11,?2))
(datetime.date(2021,?10,?11),?datetime.date(2021,?10,?19))
Process?finished?with?exit?code?0
from?pytime?import?pytime
print(pytime.this_week("2021-10-10",?clean=True))
print(pytime.next_week("2021-10-10"))
print(pytime.last_week(pytime.mother(2021),?clean=True))
#?运行结果
(datetime.date(2021,?10,?4),?datetime.date(2021,?10,?10))
(datetime.date(2021,?10,?11),?datetime.date(2021,?10,?19))
(datetime.date(2021,?4,?26),?datetime.date(2021,?5,?3))
Process?finished?with?exit?code?0
clean 是清除操作,传True 就会获取干净的周(默认是False );pytime.mother(2021) 是获取2021年的母亲节日期
clean 传True 就取到当天,如果不传就多取一天,示例如下:
from?pytime?import?pytime
print(pytime.this_week("2021-10-10"))
print(pytime.this_week("2021-10-10",?clean=True))
#?运行结果
(datetime.date(2021,?10,?4),?datetime.date(2021,?10,?11))
(datetime.date(2021,?10,?4),?datetime.date(2021,?10,?10))
Process?finished?with?exit?code?0
获取月
from?pytime?import?pytime
print(pytime.this_month())
print(pytime.next_month())
print(pytime.last_month())
#?运行结果
(datetime.date(2021,?10,?1),?datetime.date(2021,?11,?1))
(datetime.date(2021,?11,?1),?datetime.date(2021,?12,?1))
(datetime.date(2021,?9,?1),?datetime.date(2021,?10,?1))
Process?finished?with?exit?code?0
from?pytime?import?pytime
print(pytime.this_month("2021-10-21",?clean=True))
print(pytime.next_month("2021-10-21"))
print(pytime.last_month("2021-10-21"))
#?运行结果
(datetime.date(2021,?10,?1),?datetime.date(2021,?10,?31))
(datetime.date(2021,?11,?1),?datetime.date(2021,?12,?1))
(datetime.date(2021,?9,?1),?datetime.date(2021,?10,?1))
Process?finished?with?exit?code?0
clean 传True ,就是默认取到月底,如果不传就是取下个月的月初,示例如下:
from?pytime?import?pytime
print(pytime.this_month("2021-10-21"))
print(pytime.this_month("2021-10-21",?clean=True))
#?运行结果
(datetime.date(2021,?10,?1),?datetime.date(2021,?11,?1))
(datetime.date(2021,?10,?1),?datetime.date(2021,?10,?31))
Process?finished?with?exit?code?0
固定节日
from?pytime?import?pytime
#?父亲节
print(pytime.father())
#?母亲节
print(pytime.mother())
#?复活节
print(pytime.easter())
#?德国的父亲节
print(pytime.vatertag())
from?pytime?import?pytime
print(pytime.father("2020"))
print(pytime.mother("2020"))
print(pytime.easter("2020"))
print(pytime.vatertag("2020"))
计算和获取间隔时间
from?pytime?import?pytime
#?获取间隔时间
print(pytime.days_range('2015-5-17',?'2015-5-23'))
#?时间计算
print(pytime.count('2021-10-20?23:23:23',?pytime.today()))
#?运行结果
[datetime.date(2015,?5,?23),?datetime.date(2015,?5,?22),?datetime.date(2015,?5,?21),?datetime.date(2015,?5,?20),?datetime.date(2015,?5,?19),?datetime.date(2015,?5,?18),?datetime.date(2015,?5,?17)]
-1?day,?23:23:23
Process?finished?with?exit?code?0
解析不规则的日期
from?pytime?import?pytime
print(pytime.parse('April?3rd?2015'))
print(pytime.parse('Oct,?1st.?2015'))
print(pytime.parse('NOV21th2015'))
print(pytime.parse('2015517'))
print(pytime.parse('5/17/15'))
print(pytime.parse('92-11-2'))
print(pytime.parse(1432310400))???????#?支持所有函数的时间戳
#?运行结果
2015-04-03
2015-10-01
2015-11-21
2015-05-17
2015-05-17
1992-11-02
2015-05-23?00:00:00
Process?finished?with?exit?code?0
以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;非常期待你的点赞和分享哟,谢谢!
未完,待续…
一直都在努力,希望您也是!
|