-4

I have a string time that's in %H:%M:%S %b %d %Y format. What is the best way I can compare current time with that given time and get the difference in minutes? I'm thinking parse each part in the string and compare with the parsed string of current time. There has to be a better way than this

Csfcodejs
  • 3
  • 1
  • 4

2 Answers2

1

You're right, there's certainly an easier way. You may want to try this approach:

  • Convert the given string into a datetime object. Click here for info on Python's datetime module. See also this answer.
  • Generate the current time as another datetime object.
  • Get the time difference between the two. This is just a matter of subtracting one from the other. See this answer for an example.
  • Convert this to minutes. This is a little trickier. If your two times are a and b, then c = a-b gives you a new datetime object representing the difference. You can take the values c.days, c.seconds and c.microseconds, then mathematically convert each of the values to minutes, and add them all up.
Bill M.
  • 1,388
  • 1
  • 8
  • 16
0

Best way is to parse your string into datetime object with strptime an operate on that level.

Slam
  • 8,112
  • 1
  • 36
  • 44