我想循環使用自給定開始時間以來的月份,並打印第一天和最後一天。我可以手動記錄月份和年份,並使用calendar.monthrange(year,month)來獲取天數......但這是最好的方法嗎?從開始時間開始通過python月循環
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)