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)
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
.
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)
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
.