I am trying to find a more concise way to produce a range of dates between two values. I want this function to be similar to the built in range function in python.
#Range Function
>>>range(1,5)
[1,2,3,4]
This is what I currently have written:
def as_datetime(date_string):
try:
return datetime.datetime.strptime(date_string, '%d/%m/%Y')
except ValueError:
# The date string was invalid
return None
x = []
start_date = as_datetime('12/07/2013')
end_date = as_datetime('16/07/2013')
total_days = (end_date - start_date).days + 1
for day_number in range(total_days):
cd = (start_date + dt.timedelta(days = day_number)).date()
x.append(cd.strftime('%d/%m/%Y'))
#Output
>>>Print x
['12/07/2013', '13/07/2013', '14/07/2013', '15/07/2013', '16/07/2013']
To me it seems like a lot of code for a basic function. That would be great if you could assist me in finding/amending some code to perform this action.