改用 arrow 模块,更方便快捷
last_month_first_day = arrow.now().shift(months=-1).floor('month').format('YYYYMMDD')
last_month_last_day = arrow.now().shift(months=-1).ceil('month').format('YYYYMMDD')
now = datetime.datetime.now()
this_month_start = datetime.datetime(now.year, now.month, 1)
this_month_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1])