How do I tell the time difference in minutes between two datetime
objects?
views:
429answers:
2
+6
A:
Just subtract one from the other. You get a timedelta
object with the difference.
>>> import datetime
>>> d1 = datetime.datetime.now()
>>> d2 = datetime.datetime.now() # after a 5-second or so pause
>>> d2 - d1
datetime.timedelta(0, 5, 203000)
You can convert dd.days
, dd.seconds
and dd.microseconds
to minutes.
Vinay Sajip
2009-08-28 09:06:47
+8
A:
>>> import datetime
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> c = b - a
datetime.timedelta(0, 8, 562000)
>>> divmod(c.days * 86400 + c.seconds, 60)
(0, 8) # 0 minutes, 8 seconds
SilentGhost
2009-08-28 09:08:13
Reference: http://docs.python.org/library/datetime.html#datetime-objects. Read "supported operations".
S.Lott
2009-08-28 10:08:40