It was unexpectedly annoying, so make a note of it. Use a third-party module called python-dateutil. I thought it could be calculated with datetime.timedelta, but it didn't seem to support the calculation of the month.
pip install python-dateutil
from datetime import datetime, date, timedelta
from dateutil.relativedelta import relativedelta
today = datetime.today()
previous_month = today - relativedelta(months=1)
print(previous_month.month)
12
Recommended Posts