Adding days to a date in Python
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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)
With date Objects (No Time Component)
timedelta Supports Multiple Units
| Parameter | Meaning |
days | Calendar days |
seconds | Seconds |
microseconds | Microseconds |
milliseconds | Milliseconds |
minutes | Minutes |
hours | Hours |
weeks | Weeks (= 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:
Install with: pip install python-dateutil
Method 3: pandas Timestamp and DateOffset
Adding Business Days
Exclude weekends (and optionally holidays):
With numpy:
Generating Date Ranges
Working with Strings
Timezone-Aware Date Arithmetic
Common Pitfalls
- No
monthsparameter in timedelta:timedelta(months=1)raisesTypeError. Months have variable lengths. Usedateutil.relativedelta(months=1)instead. - DST transitions: Adding
timedelta(days=1)to a timezone-aware datetime may not give exactly 24 hours during DST transitions. Usedateutil.relativedeltafor DST-safe day arithmetic. - Mutable vs immutable:
dateanddatetimeobjects are immutable.today + timedelta(days=5)returns a new object — it does not modifytoday. - Comparing date and datetime:
date(2026, 3, 1) == datetime(2026, 3, 1)raisesTypeErrorin 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. Userelativedelta(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)ornumpy.busday_offsetfor business day arithmetic timedeltadoes not support months — userelativedeltainstead- Parse strings with
strptime, add days, format back withstrftime - Date and datetime objects are immutable — arithmetic returns new objects
Related reading
- adding directory to sys.path /PYTHONPATH
- Adding gaussian noise to a dataset of floating points and save it python
- Adding meta-information/metadata to pandas DataFrame
- adding noise to a signal in python
- Adding type-hinting to functions that return boto3 objects?
- Age from birthdate in python
- AKS Primes algorithm in Python
- Algorithm for generating a 3D Hilbert space-filling curve in Python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.