1

Python has a timedelta function to modify a datetime object by reducing or adding weeks, days, hours, minutes or seconds and -that is the nice part!- handle the overflows from seconds-minutes-hours-days-weeks and vice versa.

Question: I have a datetime object where I also want to add months and years and days (or subtract), without me doing the complicated arithmetic around flowing into another year, dealing with February 29th's, 30/31 days in a month etc. Any suggestions - I did not find something where I can do with months and years the same as timedelta does with weeks, days, hours, minutes etc...

If it doesn't exist, does anyone know of a piece of code which does this nicely? And whether I can 'borrow' that?

THANKS !

ToniE
  • 11
  • 4
  • You want to use [`relativedelta`](https://dateutil.readthedocs.io/en/stable/relativedelta.html) from the [`dateutil` project](https://pypi.org/project/python-dateutil/). – Martijn Pieters Oct 19 '18 at 15:14
  • That's it I was looking for...! Bedankt Martijn! Thanks Brad! – ToniE Oct 19 '18 at 17:19

0 Answers0