How to get a list of dates

Let’s say we have two different dates and we want to make a list of all dates that are between them. Here is an easy way to do it:

import datetime

date1 = datetime.datetime.strptime("2015-04-05", "%Y-%m-%d").date()
date2 = datetime.datetime.strptime("2015-06-30", "%Y-%m-%d").date()

DATES = [str(date1+datetime.timedelta(days=i)) for i in range((date2 - date1).days + 1) if date1+datetime.timedelta(days=i) <= date2]

 

Leave a Reply

Your email address will not be published. Required fields are marked *