如何创建两个变量。1 -上个月的变量2 -过去 10 个月的变量就像代表 TODAY - 10 个月的日期一样,例如:2020/02/05 - 10 months = 2019/04/05.我的代码:import datime# 1- last monthmonth = datetime.datetime.now().monthyear = datetime.datetime.now().yearlast_month = f"{year}/{month-1}/01">>>"2020/01/05"# Past 12 monthspast_10month = datetime.datetime.today()past_10month = f"{past_10month -285}">>># DO NOT WORK# EXPECTED RESULT:# "2019/04/05"
1 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
from datetime import datetime
from dateutil.relativedelta import relativedelta
dt = datetime.now()
last_month = (dt - relativedelta(months=1)).date()
last_year = (dt - relativedelta(months=10)).date()
输出:
2020-01-05
2019-04-05
添加回答
举报
0/150
提交
取消