Created
January 24, 2017 07:43
-
-
Save juanmhidalgo/e3c6e12e789ddfabf43ff92fe1945f51 to your computer and use it in GitHub Desktop.
Monthly iterate between two dates
This file contains hidden or 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 calendar | |
from datetime import datetime, timedelta | |
def monthly_range(dt_start, dt_end): | |
forward = dt_end >= dt_start | |
finish = False | |
dt = dt_start | |
while not finish: | |
yield dt.date() | |
if forward: | |
days = days_in_month(dt) | |
dt = dt + timedelta(days=days) | |
finish = dt > dt_end | |
else: | |
_tmp_dt = dt.replace(day=1) - timedelta(days=1) | |
dt = (_tmp_dt.replace(day=dt.day)) | |
finish = dt < dt_end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment