python获取一段时间的日期

获取一段时间的方法

import datetime

def getDateList(start_date, end_date):
    date_list = []
    start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    date_list.append(start_date.strftime('%Y-%m-%d'))
    while start_date < end_date:
        start_date += datetime.timedelta(days=1)
        date_list.append(start_date.strftime('%Y-%m-%d'))
    return date_list

运行示例

date_list=getDateList("2020-10-10","2020-10-15")
print(date_list)

输出:

['2020-10-10', '2020-10-11', '2020-10-12', '2020-10-13', '2020-10-14', '2020-10-15']
posted @ 2020-10-14 14:21  JunCode  阅读(315)  评论(0编辑  收藏