Skip to content

Instantly share code, notes, and snippets.

@robcowie
Last active March 2, 2020 10:44
Show Gist options
  • Save robcowie/919fceb3a6517a361554ddf170c0202b to your computer and use it in GitHub Desktop.
Save robcowie/919fceb3a6517a361554ddf170c0202b to your computer and use it in GitHub Desktop.
Generate date range from start date for N days
import datetime as dt
import operator as op
def date_iterator(from_date, days, reverse=False):
func = op.sub if reverse else op.add
return (func(from_date, dt.timedelta(days=d)) for d in range(days))
def date_range(from_date, to_date, inclusive=True):
days = (to_date - from_date).days
if inclusive:
days += 1
return date_iterator(from_date, days)
if __name__ == '__main__':
start_date = dt.datetime(2017, 3, 18)
days = 100
for d in date_iterator(start_date, 200, reverse=True):
print(d)
end_date = dt.datetime(2017, 3, 18)
for d in date_range(start_date, end_date, inclusive=True):
print(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment