Iterating through a range of dates in Python

后端 未结 23 1356
醉酒成梦
醉酒成梦 2020-11-22 04:40

I have the following code to do this, but how can I do it better? Right now I think it\'s better than nested loops, but it starts to get Perl-one-linerish when you have a ge

23条回答
  •  逝去的感伤
    2020-11-22 04:57

    This might be more clear:

    from datetime import date, timedelta
    
    start_date = date(2019, 1, 1)
    end_date = date(2020, 1, 1)
    delta = timedelta(days=1)
    while start_date <= end_date:
        print (start_date.strftime("%Y-%m-%d"))
        start_date += delta
    

提交回复
热议问题