How do I calculate the date six months from the current date using the datetime Python module?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Calculating "six months from today" sounds simple, but months are not a fixed number of days. datetime.timedelta cannot add calendar months directly, so the correct solution depends on whether you want true month arithmetic or an approximation such as 180 days. For most business logic, you want true calendar months.
Why timedelta Is Not Enough
timedelta handles days, seconds, and smaller fixed units. Months are variable in length, so this does not work reliably:
Adding 180 days is not the same as adding six calendar months. Some month ranges cross February, leap years, or 30-day months. If you care about calendar meaning, use month-aware logic.
Recommended Approach: relativedelta
The easiest month-aware solution is dateutil.relativedelta. The python-dateutil package is not part of the standard library, but it is widely used and solves this problem correctly.
This handles end-of-month behavior intelligently. For example:
Output:
That is usually what people mean by "one month later" or "six months later." The day is preserved when possible, and if the target month is shorter, the result lands on the last valid day of that month.
Pure Standard Library Option
If you cannot use python-dateutil, you can implement month arithmetic with calendar.monthrange.
This helper:
- rolls the month forward across year boundaries
- clamps the day to the last valid day of the target month
- works for common calendar-date scenarios
For many codebases, this is a good fallback if external dependencies are undesirable.
datetime Versus date
If you also care about time-of-day, use datetime instead of date. The same idea applies, but you need to preserve hours, minutes, and timezone handling.
With dateutil:
If your application stores timezone-aware datetimes, keep them timezone-aware during the calculation. Adding months to a naive local datetime can create ambiguity later when the value moves across systems with different timezone assumptions.
Be Clear About The Business Rule
Different domains mean different things by "six months from now":
- subscription billing often means same day-of-month when possible
- legal or compliance rules may define month boundaries differently
- reporting systems may want "end of the month six months ahead"
For example, some business rules treat 2024-08-31 + 6 months as the last day of February. Others may prefer the first day of the following month for special cases. The code should reflect the real rule rather than relying on a vague phrase.
Testing Edge Cases
Month arithmetic bugs usually appear around boundary dates, so test them directly:
A few explicit tests are much better than assuming the same logic works for every month length.
Common Pitfalls
- Using
timedelta(days=180)and calling it "six months." - Thinking
python-dateutilis part of the standard library. It is an external package. - Forgetting end-of-month behavior such as January 31 or August 31.
- Mixing naive and timezone-aware
datetimevalues in the same workflow. - Writing month arithmetic once and never testing leap-year and month-end cases.
Summary
- '
timedeltacannot add true calendar months.' - For most real applications, use
relativedelta(months=6)frompython-dateutil. - If you need a standard-library-only solution, compute the target year, month, and last valid day manually.
- End-of-month behavior is the main source of surprises, so test it directly.
- Decide whether you want calendar months or an approximate day count before writing the code.

