At first I tried using a timedelta but it doesn't accept months as an argument, so my next working example is this:
from datetime import datetime
current_date = datetime.now()
months = []
for month_adjust in range(2, -3, -1): # Order by new to old.
# If our months are out of bounds we need to change year.
if current_date.month + month_adjust <= 0:
year = current_date.year - 1
elif current_date.month + month_adjust > 12:
year = current_date.year + 1
else:
year = current_date.year
# Keep the months in bounds.
month = (current_date.month + month_adjust) % 12
if month == 0: month = 12
months.append(current_date.replace(year, month=month, day=1))
Is there a cleaner way?