Python日期时间arrow库

前言

Python标准库实现的时间日期很麻烦,使用了arrow第三方库之后,简洁直观。

安装方法

$ pip install -U arrow

使用例子

#!/usr/bin/python
# coding=utf-8

__author__ = 'jaysonzwj'

import arrow


def test():
    # 获取当前时间 eg:2019-09-23T23:05:01.144662+08:00
    a = arrow.now()
    print(a)
    # 格式化时间
    print(a.format("YYYY-MM-DD HH:mm:ss"))
    # 获取当前日期 eg:2019-09-23
    print(a.date())
    # 获取当前时分秒 eg:23:06:50.481176
    print(a.time())

    # 获取上一个月
    last_month = a.shift(months=-1).format("YYYYMMDD")
    print(last_month)

    # 获取当月最后一天 eg:2019-09-30
    lastday_month = a.ceil('month').date()
    print(lastday_month)

    # 获取unix时间戳,长度10位
    ten_timestamp = a.timestamp
    print(ten_timestamp)

    # unix 时间戳转成对应时间 eg: 1535113845  to  2018-08-24 12:30:45
    since_time = arrow.get(ten_timestamp)
    # utc 时间 eg : 2019-09-23 15:22:04 差8小时
    print(since_time.format("YYYY-MM-DD HH:mm:ss"))
    # 转成本地时间 eg : 2019-09-23 23:22:04
    print(since_time.to('local').format("YYYY-MM-DD HH:mm:ss"))


if __name__ == '__main__':
    test()

GitHub

https://github.com/crsmithdev/arrow/

你可能感兴趣的:(Python日期时间arrow库)