Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timedelta - datetime.now
#1
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
buran write Mar-17-2021, 03:54 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert timedelta to specified format Planetary_Assault_Systems 3 3,172 Jun-27-2021, 01:46 PM
Last Post: snippsat
  Timedelta - datetime.now PoduShychka 6 3,474 Mar-17-2021, 06:34 PM
Last Post: PoduShychka
  timedelta object cannot be interpreted as integer palladium 5 13,264 Jan-30-2020, 02:54 PM
Last Post: palladium
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,591 Sep-18-2019, 08:32 AM
Last Post: buran
  TypeError: unsupported operand type(s) for *: 'datetime.timedelta' and 'float' chris0147 2 37,605 May-01-2018, 07:20 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020