Categories
Uncategorized

Range of Dates – Python

This short script will help you create a range of dates that look like this 201203,201205,201306....

# set the date range from the minimum to the maximum
# starting with a list of string dates

def _set_date_range(list_dates):
    
    # convert all to integers
    list_int_dates = [int(x) for x in list_dates]

    # find minimum
    int_date = min(list_int_dates)

    # output list
    list_output_dates = []
    
    while int_date < max(list_int_dates):
        list_output_dates.append(int_date)

        # get the last two digits (month)
        int_month = int(str(int_date)[-2:])

        if int_month<12:
            int_date+=1

        elif int_month==12:
            int_date+=89

    # return output list
    return list_output_dates

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s