I am playing around with the datetime API and i am trying to add a full year to today’s date. For some reason the code is throwing an error. This is what my code looks like
from datetime import datetime, timedelta
print(datetime.today().date() + timedelta(months=12))
This is giving me this error. The error is
TypeError: 'months' is an invalid keyword argument for __new__()
I don’t know if i am missing something in this code.
I have tried just initializing timedelta in another variable without any arguments and it works. But when i add argument thats when it breaks. This is what i tried.
test = timedelta()
dark dayone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
Use the datetime replace()
function instead.
from datetime import datetime
today = datetime.today().date()
this_day_next_year = today.replace(year=today.year+1)
print(this_day_next_year)
This will produce unexpected results if today’s date does not exist in the next year (i.e. if you run this on Feb 29).