Generate a range of dates between start_date and end_date in pure Python using a generator or list comprehension with datetime.date and datetime.timedelta(days=1)—specifically [start + timedelta(days=i) for i in range((end - start).days + 1)] for an inclusive range—or call pd.date_range(start, end, freq="D") when working in Pandas or needing business days (freq="B"), month ends (freq="ME"), or hourly intervals (freq="h").

Basic Example

from datetime import date, timedelta
import pandas as pd

start_date = date(2026, 11, 25)
end_date = date(2026, 11, 30)

# 1. Recommended (Pure Python, inclusive): List comprehension with `timedelta`
num_days = (end_date - start_date).days
date_list = [start_date + timedelta(days=i) for i in range(num_days + 1)]
print(date_list)
# Output: [datetime.date(2026, 11, 25), datetime.date(2026, 11, 26), ..., datetime.date(2026, 11, 30)]

# 2. Memory-efficient lazy generator for huge multi-year date spans:
def iter_dates(start: date, end: date, step_days: int = 1):
    current = start
    step = timedelta(days=step_days)
    while current <= end:
        yield current
        current += step

for d in iter_dates(start_date, end_date, step_days=2):
    print(d.isoformat())
# Output: 2026-11-25, 2026-11-27, 2026-11-29

# 3. Recommended for DataFrames & custom frequencies (`pd.date_range`):
pd_dates = pd.date_range(start="2026-11-25", end="2026-11-30", freq="D")
print(pd_dates.strftime("%Y-%m-%d").tolist())
# Output: ['2026-11-25', '2026-11-26', '2026-11-27', '2026-11-28', '2026-11-29', '2026-11-30']

Key Details & Edge Cases

  • Inclusive (+ 1) vs. half-open ([start, end)) boundaries:
    • (end_date - start_date).days computes the difference, so range((end - start).days + 1) includes end_date (matching pd.date_range(..., inclusive="both")). Omit + 1 (or pass inclusive="left" in Pandas) when you want a half-open interval [start_date, end_date).
  • Reversed ranges (start_date > end_date):
    • When start_date > end_date, (end_date - start_date).days is negative, so range(num_days + 1) cleanly returns an empty list []. To step backward from start_date down to end_date, use range(0, (end_date - start_date).days - 1, -1).
  • Prefer datetime.date over datetime.datetime for calendar dates to avoid DST bugs:
    • Adding timedelta(days=1) on timezone-aware datetime objects across a Daylight Saving Time transition can shift local wall-clock hours (00:00 $\rightarrow$ 01:00 or 23:00). Using pure datetime.date objects avoids DST shifts completely for calendar-day iteration.
  • Month-by-month or year-by-year steps (timedelta does NOT accept months= or years=):
    • datetime.timedelta supports at most weeks=, days=, hours=, minutes=, and seconds=. For monthly or yearly date ranges, use pd.date_range(start, end, freq="MS") (month start) / freq="ME" (month end) or dateutil.rrule.rrule(MONTHLY, dtstart=start, until=end).
  • NumPy np.arange alternative (dtype="datetime64[D]"):
    • When building vectorized NumPy arrays without Pandas, np.arange("2026-11-25", "2026-12-01", dtype="datetime64[D]") generates contiguous 64-bit date arrays in C (note: np.arange is exclusive of the stop date, so pass end + 1 day).

Comparison of Date-Range Generation Methods in Python

Method Inclusive of end by Default? Supports Business Days / Months / Hours? Dependencies Best Used When
[start + timedelta(days=i) for i in range((end - start).days + 1)] (Recommended) Yes (with + 1) Fixed day/week/hour steps only None (standard library) Generating standard calendar dates in zero-dependency Python scripts and APIs.
iter_dates(start, end) generator Yes (while current <= end) Fixed timedelta steps None (standard library) Streaming decades/centuries of dates without allocating a full list in memory.
pd.date_range(start, end, freq="D") (Recommended for analytics) Yes (configurable via inclusive=) Yes ("D", "B", "W", "MS", "ME", "h", "15min") pandas Time-series indexing, resampling, business-day calendars, and DataFrame reindexing.
np.arange(start, end_plus_1, dtype="datetime64[D]") No (half-open [start, stop)) Fixed calendar/time units ([D], [W], [M], [h]) numpy Fast vectorized NumPy date arrays without importing Pandas.
  • @Ronan
    link
    English
    1•11 hours ago

    You can generate a range of dates in Python using the built-in datetime module.

    Example

    from datetime import date, timedelta
    
    start_date = date(2026, 11, 25)
    end_date = date(2026, 11, 30)
    
    current_date = start_date
    
    while current_date <= end_date:
        print(current_date)
        current_date += timedelta(days=1)
    

    Output:

    2026-11-25
    2026-11-26
    2026-11-27
    2026-11-28
    2026-11-29
    2026-11-30
    

    Using Pandas

    You can also use pd.date_range() if you are working with Pandas.

    import pandas as pd
    
    dates = pd.date_range("2026-11-25", "2026-11-30")
    
    print(dates)
    

    Explanation:

    • start_date defines the first date.
    • end_date defines the last date.
    • timedelta(days=1) moves to the next day.
    • pd.date_range() generates dates automatically.

    Note: Both methods include the start and end dates.