前言

1)datetime
2)arrow

datetime

获取当前日期
    from datetime import datetime
    today = datetime.today()
    print(today)
    #datetime.datetime(2018, 9, 8, 22, 32, 46)
转换输出的时间格式
    today.strftime('%Y-%m-%d')
    #'2018-09-08'
字符转为时间
    datetime.strptime('2018-09-08','%Y-%m-%d')        # 方法一
字符串转化日期对象
    dt = 20180908
    datetime.strptime(str(dt),'%Y%m%d')

安装arrow库

获取当前时间arrow对象
import arrow
    utc = arrow.utcnow()  # 获取世界标准时间
    <Arrow [2018-06-07T09:37:28.989983+00:00]>
    
    utc = arrow.now()  # 获取当前本地时间
    <Arrow [2018-06-07T17:40:19.019529+08:00]>
    
    arrow.now('US/Pacific')  # 获取指定时区的时间
    <Arrow [2018-06-07T02:41:54.815029-07:00]>
将时间arrow对象转化为时间戳
    utc.timestamp#但是3.7+后需要使用utc.int_timestamp;否则返回的是一个对象
将时间戳转化为arro
    # 时间戳可以是int,float或者可以转化为float的字符串
    arrow.get(1519534533)         
    <Arrow [2018-02-25T04:55:33+00:00]>
将时间转化成时间字符串
    utc.format()
    u'2017-02-01 17:00:42+08:00'        # 输出
将字符串转化成arrow
    arrow.get('2018-02-24 12:30:45', 'YYYY-MM-DD HH:mm:ss')
    <Arrow [2018-02-24T12:30:45+00:00]>                # 输出
按名称或tzinfo转换为时区
    arw = arrow.utcnow()
        <Arrow [2018-06-07T11:16:51.695083+00:00]>                # 输出
    arw.to('US/Pacific')
        <Arrow [2018-06-07T04:16:51.695083-07:00]>                # 输出
时间推移a.shift(**kwargs)
    # 数值为正,向后数; 数值为负,向前数。
    a.shift(weeks=+3)    #三周后
    <Arrow [2018-03-17T21:58:04.309575+08:00]>           # 输出
     
    a.shift(days=-1)     #一天前   
    <Arrow [2018-02-23T21:58:04.309575+08:00]>                # 输出
时间替换–24小时转12小时制
    a
    <Arrow [2018-02-24T21:58:04.309575+08:00]>
    a.replace(hour=9)
    <Arrow [2018-02-24T09:58:04.309575+08:00]>

 
posted on 2022-09-26 11:54  topass123  阅读(29)  评论(0编辑  收藏  举报