I want to loop through the months since a given start time, and print the first and last day. I can manually keep track of which month and year it is, and use the calendar.monthrange(year, month) to get the number of days…but is that the best way?
from datetime import date
start_date = date(2010, 8, 1)
end_date = date.today()
# I want to loop through each month and print the first and last day of the month
# 2010, 8, 1 to 2010, 8, 31
# 2010, 9, 1 to 2010, 9, 30
# ....
# 2011, 3, 1 to 2011, 3, 31
# 2011, 4, 1, to 2011, 4, 12 (ends early because it is today)
To find the last day of a month, you can use first_of_next_month – datetime.timedelta(1). For instance: