<p>I have 2 time values which have the type <code>datetime.time</code>. I want to find their difference. The obvious thing to do is t1 - t2, but this doesn't work. It works for objects of type <code>datetime.datetime</code> but not for <code>datetime.time</code>. So what is the best way to do this?</p>

[{'answer_id': 51015, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 1, 'selected': False, 'text': '<p>It seems that this isn\'t supported, since there wouldn\'t be a good way to deal with overflows in datetime.time.  I know this isn\'t an answer directly, but maybe someone with more python experience than me can take this a little further.  For more info, see this: <a href="http://bugs.python.org/issue3250" rel="nofollow noreferrer">http://bugs.python.org/issue3250</a></p>\n'}, {'answer_id': 51023, 'author': 'chryss', 'author_id': 5169, 'author_profile': 'https://Stackoverflow.com/users/5169', 'pm_score': 3, 'selected': False, 'text': '<p>You could transform both into <a href="http://docs.python.org/lib/datetime-timedelta.html" rel="noreferrer">timedelta objects</a> and subtract these from each other, which will take care to of the carry-overs. For example:</p>\n\n<pre><code>&gt;&gt;&gt; import datetime as dt\n&gt;&gt;&gt; t1 = dt.time(23, 5, 5, 5)\n&gt;&gt;&gt; t2 = dt.time(10, 5, 5, 5)\n&gt;&gt;&gt; dt1 = dt.timedelta(hours=t1.hour, minutes=t1.minute, seconds=t1.second, microseconds=t1.microsecond)\n&gt;&gt;&gt; dt2 = dt.timedelta(hours=t2.hour, minutes=t2.minute, seconds=t2.second, microseconds=t2.microsecond)\n&gt;&gt;&gt;  print(dt1-dt2)\n13:00:00\n&gt;&gt;&gt; print(dt2-dt1)\n-1 day, 11:00:00\n&gt;&gt;&gt; print(abs(dt2-dt1))\n13:00:00\n</code></pre>\n\n<p>Negative timedelta objects in Python get a negative day field, with the other fields positive. You could check beforehand: comparison works on both time objects and timedelta objects:</p>\n\n<pre><code>&gt;&gt;&gt; dt2 &lt; dt1\nTrue\n&gt;&gt;&gt; t2 &lt; t1\nTrue\n</code></pre>\n'}, {'answer_id': 51029, 'author': 'Jason Etheridge', 'author_id': 2193, 'author_profile': 'https://Stackoverflow.com/users/2193', 'pm_score': -1, 'selected': True, 'text': "<p>Firstly, note that a datetime.time is a time of day, independent of a given day, and so the different between any two datetime.time values is going to be less than 24 hours.</p>\n\n<p>One approach is to convert both datetime.time values into comparable values (such as milliseconds), and find the difference.</p>\n\n<pre><code>t1, t2 = datetime.time(...), datetime.time(...)\n\nt1_ms = (t1.hour*60*60 + t1.minute*60 + t1.second)*1000 + t1.microsecond\nt2_ms = (t2.hour*60*60 + t2.minute*60 + t2.second)*1000 + t2.microsecond\n\ndelta_ms = max([t1_ms, t2_ms]) - min([t1_ms, t2_ms])\n</code></pre>\n\n<p>It's a little lame, but it works.</p>\n"}, {'answer_id': 51042, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 4, 'selected': False, 'text': '<p>Also a little silly, but you could try picking an arbitrary day and embedding each time in it, using <code>datetime.datetime.combine</code>, then subtracting:</p>\n\n<pre><code>&gt;&gt;&gt; import datetime\n&gt;&gt;&gt; t1 = datetime.time(2,3,4)\n&gt;&gt;&gt; t2 = datetime.time(18,20,59)\n&gt;&gt;&gt; dummydate = datetime.date(2000,1,1)\n&gt;&gt;&gt; datetime.datetime.combine(dummydate,t2) - datetime.datetime.combine(dummydate,t1)\ndatetime.timedelta(0, 58675)\n</code></pre>\n'}, {'answer_id': 108599, 'author': 'Girish', 'author_id': 19632, 'author_profile': 'https://Stackoverflow.com/users/19632', 'pm_score': 2, 'selected': False, 'text': '<p>Python has pytz (<a href="http://pytz.sourceforge.net" rel="nofollow noreferrer">http://pytz.sourceforge.net</a>) module which can be used for arithmetic of \'time\' objects. It takes care of DST offsets as well. The above page has a number of examples that illustrate the usage of pytz.</p>\n'}, {'answer_id': 108603, 'author': 'Joshua', 'author_id': 6013, 'author_profile': 'https://Stackoverflow.com/users/6013', 'pm_score': -1, 'selected': False, 'text': '<p>Retrieve the times in milliseconds and then do the subtraction.</p>\n'}, {'answer_id': 263451, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>Environment.TickCount seems to work well if you need something quick.</p>\n\n<p>int start = Environment.TickCount</p>\n\n<p>...DoSomething()</p>\n\n<p>int elapsedtime = Environment.TickCount - start</p>\n\n<p>Jon</p>\n'}]
['https://Stackoverflow.com/questions/51010', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5304/']