Python处理时间的神器-----Pendulum
import pendulum
if __name__ == '__main__':
# 1-时间获取
a1 = pendulum.now()# 现在的时间
# 2022-03-10T13:49:23.695583+08:00
a2 = pendulum.yesterday()# 昨天的时间
# 2022-03-09T00:00:00+08:00
a3 = pendulum.today() # 今天的时间
# 2022-03-10T00:00:00+08:00
a4 = pendulum.tomorrow() # 明天的时间
# 2022-03-11T00:00:00+08:00
print(a3.diff(a2).in_days()) # 计算相差多少天
#1
print(a3.diff(a2).in_hours()) # 计算相差多少小时
#24
# 2-转换字符串
a4 = pendulum.now().to_datetime_string()
# 2022-03-10 13:49:23
a5 = pendulum.now().to_date_string()
# 2022-03-10
a6 = pendulum.now().to_time_string()
# 13:49:23
# 3-类型测试
from datetime import datetime
da = pendulum.datetime(2022, 3, 10)
# True
# 4- 解析规范时间
# pendulum.from_format(date, "YYYY-MM-DD")
a7 = pendulum.from_format('2022-03-10', 'YYYY-MM-DD')
# 2022-03-10T00:00:00+00:00
a8 = pendulum.parse('2022-03-10')
# 2022-03-10T00:00:00+00:00
# 属性
a9 = pendulum.now()
print(a9.year)
#2022
print(a9.month)
# 3
print(a9.day)
# 10
print(a9.hour)
# 13
print(a9.minute)
# 49
print(a9.second)
# 23