Given today's date, what is the efficient way to retrieve the first and last date for previous 3 months (i.e. 3/1/2020'
and '3/31/2020'
; '2/1/2020'
and '2/29/2020'
; '1/1/2020'
and '1/31/2020'
)?
EDIT
For previous month's first and last, the following code is working as expected. But I am not sure how to retrieve the previous 2nd and 3rd month's first and last date.
from datetime import date, timedelta
last_day_of_prev_month = date.today().replace(day=1) - timedelta(days=1)
start_day_of_prev_month = (date.today().replace(day=1)
- timedelta(days=last_day_of_prev_month.day))
# For printing results
print("First day of prev month:", start_day_of_prev_month)
print("Last day of prev month:", last_day_of_prev_month)