xxxxxxxxxx
from datetime import datetime
dates = ["2021-10-10", "2022-02-08"]
start_date, end_date = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
month_list = [datetime.strptime('%2.2d-%2.2d' % (year, month), '%Y-%m').strftime('%b-%Y')
for year in range(start_date.year, end_date.year+1)
for month in range(start_date.month if year == start_date.year else 1,
end_date.month+1 if year == end_date.year else 13)]
print(f"Months that lie between '{dates[0]}' and '{dates[1]}' are: ")
print(*month_list, sep=", ")
print(f"Total months: {len(month_list)}")
The "month_list" list contains the months between these dates above
And in order to use the actual datetime objects one should remove the `.strftime('%b-%Y')` on line 6
xxxxxxxxxx
# How to loop for every month in a range of years
for i in range(120): #(120 months in 10 years)
print(date(2000 + i//12, i%12 + 1, 1))