0
from datetime import *

date = date.today()

Printing date gives me "2020-07-21"

How may I go about adding a year to that date?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • I'd use `d.replace(d.year + 1)`. Also, avoid naming your variable `date`,you're shadowing the `date` imported from datetime – njzk2 Jul 21 '20 at 19:26
  • 2
    Does this answer your question? [Add one year in current date PYTHON](https://stackoverflow.com/questions/15741618/add-one-year-in-current-date-python) – David Duran Jul 21 '20 at 19:26

2 Answers2

6
from datetime import *
from dateutil.relativedelta import relativedelta

date = date.today()
newDate = date + relativedelta(years=1)
Daniel E
  • 394
  • 3
  • 13
1

Try:

import  datetime as dt 
d = date.today()
dt.date(d.year + 1,d.month, d.day)
ipj
  • 3,488
  • 1
  • 14
  • 18
  • you loose the rest of the information (hour, minute, ...). Surely you mean `date(date.year + 1,date.month, date.day)` – njzk2 Jul 21 '20 at 19:26
  • Would be true if `date` contains precision of hour, minutes, sec, etc but `date.today()` has only day precision - oposite to `datetime.now()` which has full timestamp precision. – ipj Jul 21 '20 at 19:31
  • by using datetime you're adding including the time as well, which is not the question – njzk2 Jul 21 '20 at 20:33
  • Right, I've changed to `dt.date`, now no time part is added. – ipj Jul 21 '20 at 20:51