Python获取时间范围

import datetime

def dateRange(beginDate, endDate):
    dates = []
    dt = datetime.datetime.strptime(beginDate, "%Y%m%d")
    date = beginDate[:]
    while date <= endDate:
        dates.append(date)
        dt = dt + datetime.timedelta(1)
        date = dt.strftime("%Y%m%d")
    return dates

if __name__ == '__main__':
    for date in dateRange('20180201', '20180310'):
        print(date)
结果:
20180201 20180202 20180203 20180204 20180205 20180206 20180207 20180208 20180209 20180210 20180211 20180212 20180213 20180214 20180215 20180216 20180217 20180218 20180219 20180220 20180221 20180222 20180223 20180224 20180225 20180226 20180227 20180228 20180301 20180302 20180303 20180304 20180305 20180306 20180307 20180308 20180309 20180310

 

posted @ 2019-02-01 09:56  音量  阅读(1475)  评论(0编辑  收藏  举报