import datetime from dateutil.relativedelta import relativedelta t=datetime.datetime.now() #当前日期 d = datetime.date.today() print(d) # 1天前 d1= d - relativedelta(days=1) print(d1) # 一个月前 d2=d - relativedelta(months=1) print(d2) # 1年前 d3=d - relativedelta(years=1) print(d3) # 1天后 d1=d - relativedelta(days=-1) print(d1) # 一个月后 d2=d - relativedelta(months=-1) print(d2) # 1年后 d3=d - relativedelta(years=-1) print(d3)
如果报错需要先安装模块
pip install python-dateutil