-1

if my list looks like this:

['2009-10-16', '21:06:34', 'kitchen sensor', 'ON']
['2009-10-16', '21:13:22', 'kitchen sensor', 'OFF']

so item[1] = 21:06:34 and listaDivisa[j][1] = 21:13:22 in this case, and my code looks like this:

 t1 = datetime.strptime(item[1], '%H:%M:%S').time 
 t2 = datetime.strptime(listaDivisa[j][1], '%H:%M:%S').time
 timedelta = (t2 - t1).seconds

why i got this error and how do i solve it: TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'builtin_function_or_method'

thank you!

Ari
  • 3
  • 1
  • 4
  • `item[1] = 21:06:34` gives an error. Do you mean `item[1] = "21:06:34"`? –  Jul 27 '22 at 15:38
  • 2
    `.time()` instead of `.time`...... – Tomerikoo Jul 27 '22 at 15:39
  • [How do I find the time difference between two datetime objects in python?](https://stackoverflow.com/q/1345827/6045800) – Tomerikoo Jul 27 '22 at 15:50
  • @SembeiNorimaki yes sorry – Ari Jul 28 '22 at 07:43
  • @Tomerikoo i tried using .time() but it doesn't resolve anything, the link you shared doesn't respond to my question – Ari Jul 28 '22 at 07:44
  • It does resolve the error in your question. If you have a new error you should ask about that... And with a little research you will solve your problem. Subtracting datetimes is a very popular topic on the internet, and here in Stack Overflow – Tomerikoo Jul 28 '22 at 07:47
  • @Tomerikoo i can't load a screenshot of my code, i tried .time() just now and it gives me the same error, pointing on "timedelta = (t2 - t1).seconds" it reports "Class 'time' does not define '__sub__', so the '-' operator cannot be used on its instances". i'm searching for something that helps me from a week now on stackoverflow, you can see i've posted more than one question about the topic because everything i find it just doesn't help me – Ari Jul 28 '22 at 07:53
  • Again, a bit of research will get you where you need: The question I linked shows how to subtract two `datetime.now()` objects. `now()` returns a `datetime` object. `time()` returns a `time` object. And on the other hand, `strptime` also returns a `datetime` object. So all you need to do is remove the `.time()` completely... – Tomerikoo Jul 28 '22 at 08:05
  • @Tomerikoo but i don't need the local current time, i need my times stored in my list... the one who posted a reply under this question also told me to remove .time() but if i remove it, nothing is printed because datetime is data value + time value and i only need time so it associates to my time values a default data value (1900-01-01) – Ari Jul 28 '22 at 08:20
  • Then again, ***this*** should be your question. Either [edit] this one or delete it and ask a new proper one, according to [ask], containing a [mre] – Tomerikoo Jul 28 '22 at 08:28

1 Answers1

0

If you want to calculate timedelta value, you don't need the time function here. datetime.strptime converts string values to time.

from datetime import datetime

a = '21:06:34'
b = '21:13:22'

t1 = datetime.strptime(a, '%H:%M:%S')
t2 = datetime.strptime(b, '%H:%M:%S')
timedelta = (t2 - t1).seconds

print(timedelta)

Output:

408
Zorojuro
  • 71
  • 5
  • thanks for your reply, i tried doing this and in fact it doesn't give me any errors but it prints wrong value (1900-01-01 00:01:04, default python timestamp) – Ari Jul 28 '22 at 07:47
  • Output in my example seems right... Can you share more details? – Zorojuro Jul 28 '22 at 08:00
  • it's like the code associates a default data value (1900-01-01) near my time value because if i'm not wrong, datatime is data value + time value but i only need something that can convert time, not also a data value. in fact, with your snippet my codes doesn't print anything because it goes in a loop i think – Ari Jul 28 '22 at 08:16