Python Forum

Full Version: Timedelta - datetime.now
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've got a problem.
So this is what I want to make .

from datetime import timedelta
from datetime import datetime

a = timedelta(hours=10 , minutes=10)
b = datetime.now

print (a-b)
It gives this error:
Error:
TypeError: unsupported operand type(s) for -: 'datetime.timedelta' and 'builtin_function_or_method'
Please help
You have two problems.

b you have set to the method instead of calling the method. You need to add parentheses to the end so that the now method is called.

When you add the elements they can be in any order, but when subtracting you must subtract the delta from the datetime, not the datetime from the delta. You have that backward. Changing those two things give you this. (along with a minor change to the imports):

from datetime import timedelta, datetime

a = timedelta(hours=10 , minutes=10)
b = datetime.now()

print (b-a)
Output:
2021-03-16 22:37:59.419331