码迷,mamicode.com
首页 > 编程语言 > 详细

Python获取时间范围

时间:2019-02-01 11:22:59      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:div   end   获取   time   append   结果   ftime   for   date   

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

 

Python获取时间范围

标签:div   end   获取   time   append   结果   ftime   for   date   

原文地址:https://www.cnblogs.com/yinliang-liang/p/10344847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!