# 获取日期加减之后的日期方法一 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')