Creating a range of dates in Python

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

    A monthly date range generator with datetime and dateutil. Simple and easy to understand:

    import datetime as dt
    from dateutil.relativedelta import relativedelta
    
    def month_range(start_date, n_months):
            for m in range(n_months):
                yield start_date + relativedelta(months=+m)
    

提交回复
热议问题