Creating a range of dates in Python

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

    from datetime import datetime, timedelta
    from dateutil import parser
    def getDateRange(begin, end):
        """  """
        beginDate = parser.parse(begin)
        endDate =  parser.parse(end)
        delta = endDate-beginDate
        numdays = delta.days + 1
        dayList = [datetime.strftime(beginDate + timedelta(days=x), '%Y%m%d') for x in range(0, numdays)]
        return dayList
    

提交回复
热议问题