Add a datetime.timedelta(days=n) object directly to a date or datetime instance using the + operator—for example, new_dt = current_dt + timedelta(days=7)—which returns a new date or datetime object while automatically handling month lengths, leap years, and year rollovers without mutating the original object (since date and datetime are immutable).
Basic Example
from datetime import date, datetime, timedelta
# 1. Recommended: Adding days to a `date` object
start_date = date(2026, 11, 30)
next_week = start_date + timedelta(days=7)
print(next_week)
# Output: 2026-12-07
# 2. Recommended: Adding days (and optional hours/minutes) to a `datetime` object
now = datetime(2026, 11, 30, 15, 24, 0)
deadline = now + timedelta(days=10, hours=4)
print(deadline)
# Output: 2026-12-10 19:24:00
# 3. Leap-year boundary handled automatically (2028 is a leap year):
leap_check = date(2028, 2, 28) + timedelta(days=1)
print(leap_check)
# Output: 2028-02-29
Key Details & Edge Cases
- Immutability (
dt + timedelta(...)returns a new object):dateanddatetimeobjects are immutable;dt + timedelta(days=5)does not modifydtin place. Reassigndt += timedelta(days=5)(or store in a new variable) to keep the shifted value.
- Fractional days and negative days work out of the box:
timedelta(days=1.5)adds 36 hours (1 day, 12:00:00).timedelta(days=-3)subtracts 3 days cleanly (equivalent todt - timedelta(days=3)).
- Daylight Saving Time (DST) nuance on timezone-aware
datetimeobjects (zoneinfo):- Adding
timedelta(days=1)adds an exact 86,400 seconds (24 hours) of elapsed wall-clock time without re-evaluating DST offset changes automatically during arithmetic. Across a spring-forward or fall-back DST transition in local zones (likeAmerica/New_York),aware_dt + timedelta(days=1)preserves the pre-transition UTC offset (-05:00) rather than shifting to-04:00. Call.astimezone(aware_dt.tzinfo)or normalize via UTC if you need exact physical 24-hour intervals across DST boundaries.
- Adding
- Adding months or years (
dateutil.relativedeltavs.timedelta):timedeltaonly accepts fixed units (days,seconds,microseconds,milliseconds,minutes,hours,weeks)—it does not acceptmonths=oryears=because month lengths vary (28–31days). Installpython-dateutiland userelativedelta(months=1)(date(2026, 1, 31) + relativedelta(months=1)$\rightarrow$2026-02-28) when calendar-month clamping is required.
- Vectorized day addition in Pandas (
pd.to_timedelta/pd.DateOffset):- For an entire Pandas
datetime64column, addpd.to_timedelta(7, unit="D")(orpd.to_timedelta(df["offset_days"], unit="D")for per-row variable day offsets) instead of looping with Pythontimedelta.
- For an entire Pandas
Comparison of Date/Time Shifting Methods in Python
| Method | Supports days / weeks / hours? |
Supports Calendar months / years? |
Library / Dependency | Best Used When |
|---|---|---|---|---|
dt + timedelta(days=n) (Recommended) |
Yes (plus weeks, hours, minutes, seconds) |
No | Built-in (datetime) |
Adding or subtracting an exact number of days, weeks, or hours to date / datetime objects. |
dt + relativedelta(days=n, months=m) |
Yes | Yes (clamps month-end dates like Jan 31 $\rightarrow$ Feb 28) | Third-party (python-dateutil) |
Billing cycles, subscriptions, or calendar-month/year offsets alongside day offsets. |
df["dt"] + pd.to_timedelta(n, unit="D") |
Yes (scalar or per-row Series of day counts) | No (use pd.DateOffset(months=m)) |
Third-party (pandas) |
Adding fixed or row-specific numbers of days across millions of DataFrame rows at C speed. |
You must log in or register to comment.
