Creating a range of dates in Python

后端 未结 20 2124
栀梦
栀梦 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:36

    Marginally better...

    base = datetime.datetime.today()
    date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]
    

提交回复
热议问题