如何列出两个日期之间的所有每周日期?

时间:2017-02-01 07:52:25

标签: python python-3.x

让我们说,我有两个约会。

d1= date(2016,12,01)
d2= date(2017,01,31)

现在从这两个日期开始,我试图打印从d1到d2的所有每周日期,例如:

2016, 12, 01
2016, 12, 08
2016, 12, 15
2016, 12, 22
2016, 12, 29
2017, 01, 05
2017, 01, 12
2017, 01, 19
2017, 02, 26

谢谢。

1 个答案:

答案 0 :(得分:2)

import datetime

d1 = datetime.date(2016,12,01)
d2 = datetime.date(2017,01,31)

weekly = d1
while weekly <= d2:
    print weekly
    weekly = weekly + datetime.timedelta(days=7)