每日一模块-datetime和arrow

# 当前时间前多少天,月
dt = arrow.now()
t1 = dt.shift(months=-6).format("YYYY-MM-DD HH:mm:SS")
print(t1)
# 指定时间的前多少天,多少月
zd = arrow.get('2019-10-12 00:00:00', 'YYYY-MM-DD HH:mm:ss')
t2 = zd.shift(months=-6).format("YYYY-MM-DD HH:mm:SS")
print(t2)

获取几小时之前,几分钟前,几天前,几个月前,及几年前的具体时间

import datetime
import arrow


def getTime(self, flag, dayhourminute):
    '''
    获取几小时之前,几分钟前,几天前,几个月前,及几年前的具体时间 flag, 1:天;2:小时;3:分钟;4:月,5:年
    :param flag: 1:天;2:小时;3:分钟;4:月,5:年
    :param dayhourminute: 整数值
    :return: 具体时间 %Y-%m-%d %H:%M:%S
    '''
    tn = datetime.datetime.now()
    t = None
    ttime = ''
    if flag <= 1:
        if flag == 1:
            t = datetime.timedelta(days=dayhourminute)
        elif flag == 2:
            t = datetime.timedelta(hours=dayhourminute)
        elif flag == 3:
            t = datetime.timedelta(minutes=dayhourminute)
        strtime = tn - t
        ttime = strtime.strftime('%Y-%m-%d %H:%M:%S')
    else:
        dt = arrow.now()
        if flag == 4:
            ttime = dt.shift(months=-dayhourminute).format("YYYY-MM-DD HH:MM:SS")
        elif flag == 5:
            ttime = str(
                int(datetime.datetime.now().strftime("%Y")) - dayhourminute) + "-" + datetime.datetime.now().strftime(
                "%m-%d")
    return ttime

arrow模块介绍

https://blog.csdn.net/weixin_39791387/article/details/81566149
https://blog.csdn.net/soft_kind/article/details/80614896
https://blog.csdn.net/sxw19880620/article/details/90046765#datetime_22

python计算两个日期间隔天数﹑周数和指定若干天后对应的日期

https://www.cnblogs.com/evening/articles/2436293.html
https://www.cnblogs.com/caiyishuai/p/9435894.html

posted @ 2019-10-12 10:17  Alive_2020  阅读(424)  评论(0编辑  收藏  举报