Python
datetime
programming
tutorial
date manipulation

Adding days to a date in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Adding days to a date is one of the most common date manipulation tasks in Python — used for calculating deadlines, generating date ranges, scheduling, and computing expiration dates. Python's datetime module provides timedelta for date arithmetic, while third-party libraries like dateutil and pandas offer additional convenience for complex operations like adding business days or months.

Method 1: datetime.timedelta (Standard Library)

python
1from datetime import datetime, timedelta
2
3today = datetime.now()
4print(today)  # 2026-03-02 14:30:00
5
6# Add 7 days
7next_week = today + timedelta(days=7)
8print(next_week)  # 2026-03-09 14:30:00
9
10# Add 30 days
11next_month = today + timedelta(days=30)
12print(next_month)  # 2026-04-01 14:30:00
13
14# Subtract days
15last_week = today - timedelta(days=7)
16print(last_week)  # 2026-02-23 14:30:00

With date Objects (No Time Component)

python
1from datetime import date, timedelta
2
3today = date.today()
4print(today)  # 2026-03-02
5
6future = today + timedelta(days=10)
7print(future)  # 2026-03-12
8
9# Difference between dates returns timedelta
10diff = date(2026, 12, 25) - today
11print(f"Days until Christmas: {diff.days}")

timedelta Supports Multiple Units

python
1from datetime import timedelta
2
3# Add hours, minutes, seconds
4delta = timedelta(days=1, hours=6, minutes=30)
5result = datetime.now() + delta
6print(result)
7
8# Add weeks
9two_weeks = timedelta(weeks=2)
10result = datetime.now() + two_weeks
11
12# Negative delta (subtract)
13three_days_ago = datetime.now() + timedelta(days=-3)
14# Same as: datetime.now() - timedelta(days=3)
ParameterMeaning
daysCalendar days
secondsSeconds
microsecondsMicroseconds
millisecondsMilliseconds
minutesMinutes
hoursHours
weeksWeeks (= 7 days)

Note: timedelta does not support months or years because they have variable lengths (28-31 days, 365-366 days).

Method 2: dateutil.relativedelta (Months and Years)

For adding months or years, use dateutil:

python
1from datetime import datetime
2from dateutil.relativedelta import relativedelta
3
4today = datetime.now()
5
6# Add 1 month
7next_month = today + relativedelta(months=1)
8print(next_month)  # 2026-04-02
9
10# Add 1 year
11next_year = today + relativedelta(years=1)
12print(next_year)  # 2027-03-02
13
14# Add 2 months and 15 days
15future = today + relativedelta(months=2, days=15)
16print(future)  # 2026-05-17
17
18# End-of-month handling
19jan_31 = datetime(2026, 1, 31)
20feb = jan_31 + relativedelta(months=1)
21print(feb)  # 2026-02-28 — automatically clamped to last day of February

Install with: pip install python-dateutil

Method 3: pandas Timestamp and DateOffset

python
1import pandas as pd
2
3today = pd.Timestamp.now()
4
5# Add days
6future = today + pd.Timedelta(days=10)
7print(future)
8
9# Add business days (skip weekends)
10business_day = today + pd.offsets.BDay(5)
11print(business_day)  # 5 business days from now
12
13# Add months
14next_month = today + pd.DateOffset(months=1)
15print(next_month)
16
17# Generate a date range
18dates = pd.date_range(start='2026-03-01', periods=7, freq='D')
19print(dates)
20# DatetimeIndex(['2026-03-01', '2026-03-02', ..., '2026-03-07'])

Adding Business Days

Exclude weekends (and optionally holidays):

python
1from datetime import date, timedelta
2
3def add_business_days(start_date, num_days):
4    current = start_date
5    added = 0
6    while added < num_days:
7        current += timedelta(days=1)
8        if current.weekday() < 5:  # Monday=0, Friday=4
9            added += 1
10    return current
11
12today = date(2026, 3, 6)  # Friday
13print(add_business_days(today, 3))  # 2026-03-11 (Wednesday — skips Sat/Sun)

With numpy:

python
1import numpy as np
2
3# Add 5 business days
4result = np.busday_offset('2026-03-06', 5)
5print(result)  # 2026-03-13
6
7# With custom holidays
8holidays = ['2026-03-09']
9result = np.busday_offset('2026-03-06', 5, holidays=holidays)
10print(result)  # 2026-03-16 (skips the holiday too)

Generating Date Ranges

python
1from datetime import date, timedelta
2
3def date_range(start, end):
4    current = start
5    while current <= end:
6        yield current
7        current += timedelta(days=1)
8
9for d in date_range(date(2026, 3, 1), date(2026, 3, 7)):
10    print(d)
11# 2026-03-01
12# 2026-03-02
13# ...
14# 2026-03-07
15
16# As a list
17dates = list(date_range(date(2026, 3, 1), date(2026, 3, 7)))

Working with Strings

python
1from datetime import datetime, timedelta
2
3# Parse string → add days → format back
4date_str = "2026-03-01"
5dt = datetime.strptime(date_str, "%Y-%m-%d")
6new_dt = dt + timedelta(days=10)
7result = new_dt.strftime("%Y-%m-%d")
8print(result)  # "2026-03-11"
9
10# Common formats
11# "%Y-%m-%d"        → 2026-03-01
12# "%d/%m/%Y"        → 01/03/2026
13# "%m/%d/%Y"        → 03/01/2026
14# "%B %d, %Y"       → March 01, 2026

Timezone-Aware Date Arithmetic

python
1from datetime import datetime, timedelta, timezone
2
3# UTC datetime
4utc_now = datetime.now(timezone.utc)
5future = utc_now + timedelta(days=5)
6print(future)
7
8# With pytz
9import pytz
10
11eastern = pytz.timezone('US/Eastern')
12now_eastern = datetime.now(eastern)
13future = now_eastern + timedelta(days=5)
14print(future)  # Timezone is preserved
15
16# DST-aware arithmetic with dateutil
17from dateutil import tz
18
19eastern = tz.gettz('US/Eastern')
20dt = datetime(2026, 3, 8, 1, 0, tzinfo=eastern)  # Before spring forward
21next_day = dt + timedelta(days=1)
22print(next_day)  # Correctly handles DST transition

Common Pitfalls

  • No months parameter in timedelta: timedelta(months=1) raises TypeError. Months have variable lengths. Use dateutil.relativedelta(months=1) instead.
  • DST transitions: Adding timedelta(days=1) to a timezone-aware datetime may not give exactly 24 hours during DST transitions. Use dateutil.relativedelta for DST-safe day arithmetic.
  • Mutable vs immutable: date and datetime objects are immutable. today + timedelta(days=5) returns a new object — it does not modify today.
  • Comparing date and datetime: date(2026, 3, 1) == datetime(2026, 3, 1) raises TypeError in Python 3. Convert to the same type before comparing.
  • End-of-month edge cases: Adding 1 month to January 31 should give February 28 (or 29). timedelta(days=30) gives March 2. Use relativedelta(months=1) for correct month arithmetic.

Summary

  • Use datetime.timedelta(days=N) for adding days, hours, minutes, or seconds
  • Use dateutil.relativedelta(months=N) for adding months or years (handles variable-length months)
  • Use pd.offsets.BDay(N) or numpy.busday_offset for business day arithmetic
  • timedelta does not support months — use relativedelta instead
  • Parse strings with strptime, add days, format back with strftime
  • Date and datetime objects are immutable — arithmetic returns new objects

Course illustration
Course illustration

All Rights Reserved.