import datetime def pre_date(days=0, base_day=None): if base_day: base_day = base_day.strftime('%Y-%m-%d') else: base_day = datetime.datetime.now().strftime('%Y-%m-%d') return datetime.datetime.strptime(base_day, '%Y-%m-%d') - datetime.timedelta(days=days) print(pre_date(-1))