from datetime import date
y1, m1 = stlmt_date[0].year, stlmt_date[0].month
y2, m2 = stlmt_date[1].year, stlmt_date[1].month
if y1 < y2:
raise Exception('起止年月有问题,{} to {}'.format(stlmt_date[0], stlmt_date[1]))
import calendar
from datetime import datetime, timedelta
def days_in_month(dt):
'''获取某个月有多少天
'''
return calendar.monthrange(dt.year, dt.month)[1]
def monthly_range(dt_start, dt_end):
forward = dt_end >= dt_start
finish = False
dt = dt_start
while not finish:
yield dt
if forward:
days = days_in_month(dt)
dt = dt + timedelta(days=days)
finish = dt > dt_end
else:
_tmp_dt = dt.replace(day=1) - timedelta(days=1)
dt = (_tmp_dt.replace(day=dt.day))
finish = dt < dt_end