TypeError: can only concatenate str (not "relativedelta") to str

Question:

i’m trying to get next month from selected date. if i using datetime.date.today() its ok. but i need working with request date. how can i solve it.Thanks

date = request.form['date']
print(date) // 2022-11-05
// nextmonth = datetime.date.today() + relativedelta.relativedelta(months=1)
nextmonth = date + relativedelta.relativedelta(months=1)
Asked By: RK-Rohan

||

Answers:

You have to convert str to date and then use relativedelta.relativedelta.

Not sure what format the date is, I am assuming it is YYYY-MM-DD.

You can change the day and month format as per what you want.

import datetime
from dateutil import relativedelta
date = '2022-11-05'
date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
nextmonth = date + relativedelta.relativedelta(months=1)

That should give you 2022-12-05.

Answered By: python_user
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.