zoukankan      html  css  js  c++  java
  • 【Python】日期加减(获取前N天/后N天日期)

    import datetime
    
    
    class DateTool:
        @staticmethod
        def offset(date_format, offset_type, count, data=None):
            """
            日期偏移(前N天、后N天)
            @param date_format: 格式(如:%Y-%m-%d %H:%M:%S)
            @param offset_type: 偏移类型(Q-向前偏移 即前N天,H-向后偏移 即后N天)
            @param count: 偏移天数
            @param data: 偏移基准日期(如:2020-11-14 19:48:51) 若为None则当前日期
            @return: 偏移后的日期
            """
            # 时间加减
            # 获取当前日期
            if data is None:
                data = datetime.datetime.now().strftime(date_format)
            # 将时间字符串转换为 datetime 格式的时间
            today = datetime.datetime.strptime(data, date_format)
            # 计算偏移量
            if offset_type == 'Q':
                offset = datetime.timedelta(days=-count)
            elif offset_type == 'H':
                offset = datetime.timedelta(days=+count)
            else:
                print(f'偏移类型错误,预期:“Q”或“H”,实际:{offset_type}')
            # 获取修改后的时间并格式化
            re_date = (today + offset).strftime(date_format)
            return re_date
    
    
    if __name__ == '__main__':
        print(DateTool.offset('%Y%m%d', 'Q', 3))  # 20210823  (备注:今天是8.26日)
        print(DateTool.offset('%Y%m%d', 'H', 3, '20210829'))  # 20210901
    如果忍耐算是坚强 我选择抵抗 如果妥协算是努力 我选择争取
  • 相关阅读:
    Queries about less or equal elements CodeForces
    Session in BSU CodeForces
    基环树
    骑士 HYSBZ
    Valid BFS? CodeForces
    Trips CodeForces
    The writing on the wall 南京网络赛2018B题
    Building a Space Station
    Constructing Roads
    Networking
  • 原文地址:https://www.cnblogs.com/danhuai/p/15189448.html
Copyright © 2011-2022 走看看