calculate difference between two timestamps [newbie]

Günther Dietrich gd.usenet at spamfence.net
Sat Dec 17 06:20:55 EST 2011


nukeymusic <nukeymusic at gmail.com> wrote:

>I'm trying to calculate the difference in seconds between two

[...]

>>> import datetime
>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S")
>>> date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S")
>>> delta = date2 - date1
>>> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000)

For very big time differences you should consider to use the Decimal 
arithmetics (standard module Decimal) instead of integer arithmetics 
for the last line.
If you are sure, that you don't use fractional seconds, you can omit
the part with 'delta.microseconds'.



Best regards,

Günther



More information about the Python-list mailing list