Creating a range of dates in Python

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

    Here's a slightly different answer building off of S.Lott's answer that gives a list of dates between two dates start and end. In the example below, from the start of 2017 to today.

    start = datetime.datetime(2017,1,1)
    end = datetime.datetime.today()
    daterange = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
    

提交回复
热议问题