Generating all dates within a given range in python

前端 未结 9 555
青春惊慌失措
青春惊慌失措 2021-02-02 08:31

I have two string variables which contain dates in yyyy-mm-dd format as follows :

date1 = \'2011-05-03\'
date2 = \'2011-05-10\'

I want to write

9条回答
  •  日久生厌
    2021-02-02 09:22

    from dateutil import rrule, parser
    
    date1 = '2011-05-03'
    date2 = '2011-05-10'
    
    dates = list(rrule.rrule(rrule.DAILY,
                             dtstart=parser.parse(date1),
                             until=parser.parse(date2)))
    
    print dates
    

    Since dateutil is not a standard library, you will have to install it as a separate package. See the documentation for further details regarding the format (especially dayfirst and yearfirst switches).

提交回复
热议问题