Generating all dates within a given range in python

前端 未结 9 569
青春惊慌失措
青春惊慌失措 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:17

    import datetime
    real_date1 = datetime.date(*[int(x) for x in date1.split('-')])
    real_date2 = datetime.date(*[int(x) for x in date2.split('-')])
    date_range =  real_date2 - real_date1
    dates = list()
    for days in xrange(date_range.days):
        dates.append(real_date1 + datetime.timedelta(days))
    
    print dates
    

    for python 3 use range instead of xrange.

提交回复
热议问题