Generating dates within a given range in Python

First example

import datetime

date_start = datetime.datetime.today()
last_day = 30

dates = [date_start + datetime.timedelta(days=num) for num in range (0, last_day)]
print(dates)

Second example

# pip install python-dateutil

import datetime
from dateutil import rrule

date_start = datetime.datetime.today()
last_day = 30

dates = list(rrule.rrule(rrule.DAILY, count=last_day, dtstart=date_start))
print(dates)

Third example

# pip install python-dateutil

from dateutil import rrule, parser

date_start = parser.parse('2015-01-03')
date_end = parser.parse('2015-02-02')

dates = list(rrule.rrule(rrule.DAILY, dtstart=date_start, until=date_end))
print(dates)
comments powered by Disqus