get a list of date string between two date string in python(arrow)

104 views Asked by At

Basically, I have two date string and want to get a list of date string between them.

For example, input strings:

start_date = '20180427', end_date = '201800501'

result:

['20180427', '20180428', '20180429', '20180430', '20180501']

I would prefer an arrow way of doing this since I am using python arrow library for dates and times.

Solution: I came up with this using python arrow:

def get_date_in_between(start, end):
    '''
    :param start: string, in a format 'YYYYMMDD'
    :param end: string, in as fromat 'YYYYMMDD'
    :return: a list of date string between start and end(inclusive)
    '''
    start_date = arrow.get(start,'YYYYMMDD')
    end_date = arrow.get(end,'YYYYMMDD')

    date_list = []
    while start_date <= end_date:
        date_list.append(start_date.format('YYYYMMDD'))
        start_date= start_date.shift(days=1)

    return date_list

result: [u'20180427', u'20180428', u'20180429', u'20180430', u'20180501']

0

There are 0 answers