Hacker News new | past | comments | ask | show | jobs | submit login




I respect wanting or needing to do this with just the builtin datetime module. For everyone else, I recommend relativedelta from the dateutil package: https://dateutil.readthedocs.io/en/stable/relativedelta.html

e.g.:

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



I was about to ask why you didn't use a timedelta, and then found out that datetime.timedelta doesn't support anything greater than week.

It also has this incorrect example: https://docs.python.org/3/library/datetime.html#examples-of-...




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: