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.
Can someone help?
I don't understand why this doesn't work.

from datetime import datetime , timedelta

z = (timedelta(days=2 ,hours=16 , minutes = 00))
a = (timedelta(hours=0 , minutes =59 ))
c = (z+a)

b = datetime.now()
 
print (c-b)
Error:
TypeError: unsupported operand type(s) for -: 'datetime.timedelta' and 'datetime.datetime'
What exactly would you expect as result if you substract current datetime from 3h 15min time interval if you calculate by hand?
3h:15 min - 17.03.2021 19:18?
I know what would be the result if I substract 3h 15min from current datetime.
17.03.2021 19:18 - 3h 15 min = 17.03.2021 16:03
If you eliminate the outside parenthesis, you can add and subtract timedelta (). I don't know if the same is possible for datetime since they are completely different formats.

from datetime import datetime , timedelta
 
z = timedelta(days=2 ,hours=16 , minutes = 00)
a = timedelta(hours=0 , minutes =59 )
c = z+a
 
print (datetime.now())
  
print (c)
Output:
2021-03-17 11:31:57.655933 2 days, 16:59:00
(Mar-17-2021, 05:12 PM)PoduShychka Wrote: [ -> ]I need it to be as 3h 15m - datetime now . May be if its possible to subtract 3h 15m - datetime(as hours and minutes)
I need it to be as 3h 15m - datetime now . May be if its possible to subtract 3h 15m - datetime(as hours and minutes)
The 3h 15m will be never smaller then the datetime now, so i will always get something like this 00:58:00
(Mar-17-2021, 05:41 PM)PoduShychka Wrote: [ -> ]I need it to be as 3h 15m - datetime now . May be if its possible to subtract 3h 15m - datetime(as hours and minutes)
The 3h 15m will be never smaller then the datetime now, so i will always get something like this 00:58:00
Wall Wall Wall Wall Wall Wall
I asked you very specific question and you didn't answer. not something like... tell me the result you would like to get.
3h:15 min - 17.03.2021 19:18?
it is possible to do
datetime - timedelta = datetime
datetime + timedelta = datetime
datetime - datetime = timedelta

for all of the above it is clear what the result is and you can calculate the result by hand


timedelta - datetime - doesn't make sense, you cannot calculate it
I am sorry , I didn't know . Have a great day Smile