Python处理时间的神器-----Pendulum

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值