One major difference not highlighted in other answers is the presence of singular and plural nouns for each time difference primitive. While timedelta
only offers plural nouns (e.g. hours
, days
) to denote relative time difference, relativedelta
offers singular nouns as well (e.g. hour
, day
) to denote absolute time information.
This is clear from the definition of the 2 classes:
Definition: datetime.timedelta([days[, seconds[, microseconds[,
milliseconds[, minutes[, hours[, weeks]]]]]]])
Definition: relativedelta.relativedelta(self, dt1=None, dt2=None,
years=0, months=0, days=0, leapdays=0, weeks=0, hours=0, minutes=0,
seconds=0, microseconds=0, year=None, month=None, day=None,
weekday=None, yearday=None, nlyearday=None, hour=None, minute=None,
second=None, microsecond=None)
Now, exactly what does the singular form do? Singular form creates a delta which when added to a datetime
object, sets that specific date/time primitive in the datetime
object to that mentioned in the relativedelta
. Here is a small example:
>>> import datetime as dt; from dateutil.relativedelta import *
>>> NOW = dt.datetime(2018, 11, 17, 9, 6, 31)
>>> NOW
datetime.datetime(2018, 11, 17, 9, 6, 31)
>>> NOW + relativedelta(hours=1) #Simply add one hour
datetime.datetime(2018, 11, 17, 10, 6, 31)
>>> NOW + relativedelta(hour=1) #Set the hour to 01:00 am
datetime.datetime(2018, 11, 17, 1, 6, 31)
This can lead to relativedelta
being used for some interesting applications, which can be complicated to implement using timedelta
. One that quickly comes to mind is rounding-off.
An Interesting Application : Quickly Rounding off
I will now show you how relativedelta
is more expressive when doing rounding off a datetime
object to the nearest minute, hour, day etc.
Rounding off to the nearest hour:
Notice how straightforward it is to round-off using relativedelta
:
#Using `relativedelta`
NOW + relativedelta(hours=1, minute=0, second=0, microsecond=0)
#Using `timedelta`
dt.combine(NOW.date(),dt.time(NOW.hour,0,0)) + dt.timedelta(0,60*60,0)
Other more complicated rounding-offs are easily achievable using relativedelta
. However, note that all the round-offs that can be done by relativedelta
can also be done using datetime
functions and timedelta
, only in a slightly more convoluted way.