Fredrikj31
Fredrikj31

Reputation: 155

Python cannot concatenate 'str' and 'datetime.timedelta' objects

I have this code, but I got an error and could not find a soulution to the problem. I was trying to get a time and then plus i hour becuase i live in europe.

Here is my code: plus_one_hour = "21:00:00" + timedelta(hours=2)

And the error: TypeError: cannot concatenate 'str' and 'datetime.timedelta' objects

Upvotes: 5

Views: 37450

Answers (1)

Rakesh
Rakesh

Reputation: 82765

Convert your string to datetime object.

Ex:

import datetime
plus_one_hour = datetime.datetime.strptime("21:00:00", "%H:%M:%S") + datetime.timedelta(hours=2)
print(plus_one_hour.strftime("%H:%M:%S"))

Output:

23:00:00

Upvotes: 12

Related Questions