0

I have a string '16:40' and I would like to calculate the difference in minutes between this string and now. So assuming now is 16:52, then the difference would be 12 minutes.

I have the following code:

import time

tString = '16:40'
t1 = time.strptime(tString, "%H:%M")
now = time.time()
print (now - t1)

For which get the following type error:

TypeError: unsupported operand type(s) for -: 'float' and 'time.struct_time'

So I'm creating the incorrect type for now - any ideas?!

Anthony W
  • 1,289
  • 2
  • 15
  • 28
  • 1
    Possible duplicate of [Converting string into datetime](http://stackoverflow.com/questions/466345/converting-string-into-datetime) – Amin Etesamian Feb 21 '17 at 17:00

1 Answers1

1

I use to use datetime so you can do that, it's a bit different and after you have to format the date/time in the desired format but it works:

import datetime
tString = '16:40'
now=datetime.datetime.now()

difference=now- datetime.timedelta(hours=int(tString [:2]),minutes=int(tString [3:]))
Carlo 1585
  • 1,455
  • 1
  • 22
  • 40