pythondatetimetimedeltapython-dateutil

Using datetime.timedelta to add years


I am doing some time calculations in Python.

Goal:

Part of this is trying to :

Given a date, add time interval (X years, X months, X weeks), return date

ie

I looked at the datetime and datetime.timedelta docs

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)¶.

These seem to work well if I want to add a certain number of hours or weeks. However,

Problem:

E.g.

start = datetime.datetime(2000, 1, 1)
# expected output: datetime.datetime(2001, 1, 1)


# with the weeks, etc arguments given in timedelta, this fails unsurprisingly e.g 
start + datetime.timedelta(weeks = 52)

# returns datetime.datetime(2000, 12, 30, 0, 0)

Question

I have reviewed this similar question but it did not help me with this.

Any help is much appreciated!

Running Python 3.6.5 on MacOs.


Solution

  • timedelta does not support years, because the duration of a year depends on which year (for example, leap years have Feb 29).

    You could use a relativedelta instead (from PyPI package python-dateutil) which does support years and takes into account the baseline date for additions.

    >>> from dateutil.relativedelta import relativedelta
    >>> import datetime
    >>> d = datetime.date(2020, 2, 29)
    >>> d
    datetime.date(2020, 2, 29)
    >>> d + relativedelta(years=1)
    datetime.date(2021, 2, 28)