自动化测试常用脚本-获取日期加减之后的日期

# 获取日期加减之后的日期方法一
from datetime import date, timedelta


def get_date(day_off):
    """
    if n>=0,date is larger than today
    if n<0,date is less than today
    date format = "YYYY-MM-DD"
    :param day_off: 日期差
    """
    if day_off < 0:
        day_off = abs(day_off)
        return date.today() - timedelta(days=day_off)
    else:
        return date.today() + timedelta(days=day_off)


if __name__ == '__main__':
    print(get_date(30))

 

# 获取日期加减之后的日期方法二

'''日期加减'''

def date_add_days(date, days):
    date1 = time.strptime(date, "%Y-%m-%d")
    now = datetime.datetime(date1[0], date1[1], date1[2])
    delta = datetime.timedelta(days=days)
    n_days = now + delta
    return n_days.strftime('%Y-%m-%d')

 

上一篇:MyBatis详细介绍


下一篇:js中比较两个日期的大小