def get_time_range_list(startdate, enddate):
"""
获取时间参数列表
:param startdate: 起始月初时间 --> str
:param enddate: 结束时间 --> str
:return: date_range_list -->list
"""
date_range_list = []
startdate = datetime.datetime.strptime(startdate, '%Y-%m-%d')
enddate = datetime.datetime.strptime(enddate, '%Y-%m-%d')
while 1:
next_month = startdate + datetime.timedelta(days=calendar.monthrange(startdate.year, startdate.month)[1])
month_end = next_month - datetime.timedelta(days=1)
if month_end < enddate:
date_range_list.append((datetime.datetime.strftime(startdate,
'%Y-%m-%d'),
datetime.datetime.strftime(month_end,
'%Y-%m-%d')))
startdate = next_month
else:
return date_range_list
if __name__ == '__main__':
# GetAllOrder().handle('2022-01-01', '2022-01-31')
months = get_time_range_list('2022-01-01', '2022-11-01')
for month in months:
start_time = month[0]
end_time = month[1]
print(start_time, end_time)
输入两个日期,获取月份的第一天和最后一天
最新推荐文章于 2024-07-19 10:23:57 发布