Last active
March 14, 2017 18:06
-
-
Save jourdanrodrigues/e3590c97ba3067e91265d39e267c43d5 to your computer and use it in GitHub Desktop.
A simple Python function to create an iterable date range.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def date_range(start_date, end_date): | |
""" | |
Yields each date in a range of dates | |
Source for future reference: http://stackoverflow.com/a/1060330/4694834 | |
:param start_date: date to start the iteration | |
:type start_date: datetime.datetime | |
:param end_date: date to end the iteration | |
:type end_date: datetime.datetime | |
:return: iterable | |
""" | |
if start_date > end_date: | |
raise ValueError('The start date must be less than of equal the end date.') | |
# The "+ 1" is to add the end date to the iteration | |
for day in range((end_date - start_date).days + 1): | |
yield start_date + datetime.timedelta(day=day) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment