import datetime today = datetime.date.today() # 鉴于月份只能在1-12中,月份为1月时,年份-1,月份+11,即为去年12月时间 if today.month == 1: last_month = today.replace(year=today.year - 1).replace(month=today.month + 11) last_month_date = last_month.strftime("%Y-%m-%d") print(last_month_date) else: # 除开1月,其他月份按照正常逻辑处理 last_month = today.replace(month=today.month + 1) last_month_date = last_month.strftime("%Y-%m-%d") print(last_month_date)