##当天日期 today = datetime.date.today() print(today) ##获取当前月的第一天 first = today.replace(day=1) print(first) ##减一天,得到上个月的最后一天 last_month = first - datetime.timedelta(days=1) print(last_month) print(last_month.strftime("%Y%m"))