- A+
所属分类:python笔记
今天在用Python跑一个数据时,需要遍历一个日期范围,于是在网上找到如下可用的代码,记录一下。
方法1:
import datetime def main(): begin = datetime.date(2014,6,1) end = datetime.date(2014,6,7) for i in range((end - begin).days+1): day = begin + datetime.timedelta(days=i) print str(day) if __name__ == '__main__': main()
方法2:
import datetime begin = datetime.date(2014,6,1) end = datetime.date(2014,6,7) d = begin delta = datetime.timedelta(days=1) while d <= end: print d.strftime("%Y-%m-%d") d += delta
输出如下: 2014-06-01 2014-06-02 2014-06-03 2014-06-04 2014-06-05 2014-06-06 2014-06-07
原文地址:http://blog.csdn.net/wusuopubupt/article/details/29606481