Creating a range of dates in Python

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

    I know this has been answered, but I'll put down my answer for historical purposes, and since I think it is straight forward.

    import numpy as np
    import datetime as dt
    listOfDates=[date for date in np.arange(firstDate,lastDate,dt.timedelta(days=x))]
    

    Sure it won't win anything like code-golf, but I think it is elegant.

提交回复
热议问题