Easy to use the replace method.
from datetime import datetime
from dateutil.relativedelta import relativedelta
today = datetime.now().date()
print(today)
# => 2020-05-27
last_month_start = (today - relativedelta(months=1)).replace(day=1)
print(last_month_start)
# => 2020-04-01
last_month_end = today.replace(day=1) - relativedelta(days=1)
print(last_month_end)
# => 2020-04-30
Don't miss the variable name.
https://www.lifewithpython.com/2018/06/python-get-first-and-last-day-of-month.html
Recommended Posts