Skip to content

Instantly share code, notes, and snippets.

@rooty
Created December 30, 2012 22:26
Show Gist options
  • Save rooty/4415665 to your computer and use it in GitHub Desktop.
Save rooty/4415665 to your computer and use it in GitHub Desktop.
import datetime
def daterange(start, stop, step=datetime.timedelta(days=1), inclusive=True):
# inclusive=False to behave like range by default
if step.days > 0:
while start < stop:
yield start
start = start + step
elif step.days < 0:
while start > stop:
yield start
start = start + step
if inclusive and start == stop:
yield start
Как юзать ? Вот например так:
from datetime import timedelta,date
end_date = date.toady()
start_date = date.today()-timedelta(days=365)
dates = [x for x in daterange(start_date,
end_date,
step=timedelta(weeks=1),
inclusive=True)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment