I want to generate a python list containing all months occurring between two dates, with the input and output formatted as follows:
date1 = \"2014-10-10\" #
I came to a solution that uses python-dateutil and works with Python 3.8+:
https://gist.github.com/anatoly-scherbakov/593770d446a06f109438a134863ba969
def month_range(
start: datetime.date,
end: datetime.date,
) -> Iterator[datetime.date]:
"""Yields the 1st day of each month in the given date range."""
yield from itertools.takewhile(
lambda date: date < end,
itertools.accumulate(
itertools.repeat(relativedelta(months=1)),
operator.add,
initial=start,
)
)