Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timedelta - datetime.now
#1
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'
Reply
#2
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 can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
(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)
Reply
#5
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
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
I am sorry , I didn't know . Have a great day Smile
buran write Mar-17-2021, 06:39 PM:
Please, don't post your thoughts like my quote.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert timedelta to specified format Planetary_Assault_Systems 3 3,189 Jun-27-2021, 01:46 PM
Last Post: snippsat
  Timedelta - datetime.now PoduShychka 1 5,566 Mar-17-2021, 03:49 PM
Last Post: bowlofred
  timedelta object cannot be interpreted as integer palladium 5 13,284 Jan-30-2020, 02:54 PM
Last Post: palladium
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,604 Sep-18-2019, 08:32 AM
Last Post: buran
  TypeError: unsupported operand type(s) for *: 'datetime.timedelta' and 'float' chris0147 2 37,631 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