Python获取时间范围
Posted 音量
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Python获取时间范围的主要内容,如果未能解决你的问题,请参考以下文章