Python generating a list of dates between two dates

前端 未结 3 1081
栀梦
栀梦 2021-01-18 03:45

I want to generate a list of dates between two dates and store them in a list in string format. This list is useful to compare with other dates I have.

My code is g

相关标签:
3条回答
  • 2021-01-18 03:59

    You can use pd.date_range() for this:

    pd.date_range(sdate,edate-timedelta(days=1),freq='d')
    

    DatetimeIndex(['2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25',
               '2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29',
               '2019-03-30', '2019-03-31', '2019-04-01', '2019-04-02',
               '2019-04-03', '2019-04-04', '2019-04-05', '2019-04-06',
               '2019-04-07', '2019-04-08'],
              dtype='datetime64[ns]', freq='D')
    
    0 讨论(0)
  • 2021-01-18 04:09
    from datetime import date, timedelta
    
    sdate = date(2019,3,22)   # start date
    edate = date(2019,4,9)   # end date
    date_modified=sdate
    list=[sdate] 
    
    
    while date_modified<edate:
        date_modified+=timedelta(days=nbDaysbtw2dates) 
        list.append(date_modified)
    
    print(list) 
    
    0 讨论(0)
  • 2021-01-18 04:12

    You'd need to turn it into a list with strings explicitly:

    print([str(d) for d in dates_bwn_twodates(sdate,edate)])
    
    0 讨论(0)
提交回复
热议问题