1. 显示昨天(前一天)的日期
from datetime import date, timedelta yesterday_date = (date.today() + timedelta(days = -1)).strftime("%Y-%m-%d") #输出:2019-11-21 yesterday_date = (date.today() + timedelta(days = -1)).strftime("%Y%m%d") #输出:20191121
2. 显示第前 'n' 天,将 timedelta的参数 'days' 设置为 '-n'
#例如,n=10 n = 10 before_n_days = (date.today() + timedelta(days= -n)).strftime("%Y-%m-%d")