Creating a range of dates in Python

后端 未结 20 2091
栀梦
栀梦 2020-11-22 11:02

I want to create a list of dates, starting with today, and going back an arbitrary number of days, say, in my example 100 days. Is there a better way to do it than this?

20条回答
  •  情歌与酒
    2020-11-22 11:40

    import datetime    
    def date_generator():
        cur = base = datetime.date.today()
        end  = base + datetime.timedelta(days=100)
        delta = datetime.timedelta(days=1)
        while(end>base):
            base = base+delta
            print base
    
    date_generator()
    

提交回复
热议问题