Difference between two times (working ugly code, needs polish)
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sun Sep 9 11:15:50 EDT 2007
Shawn Milochik a écrit :
> I have done what I wanted, but I think there must be a much better way.
>
> Given two timestamps in the following format, I just want to figure
> out how far apart they are (in days, seconds, whatever).
>
> Format:
>
> YYYY-MM-DD_MM:SS
>
> Example:
> 2007-09-11_16:41
>
>
> It seems to me that to do what I want, I need to convert a string into
> a number (time.mktime()), such as this: 1189543487.0
>
> Once I have that, I can just subtract one from the other and do
> whatever I want. The ugly part is converting something like
> 2007-09-11_16:41 to the numeric equivalent.
>
> Below is my code. Any suggestions?
import time
print time.mktime(time.strptime("2007-09-11_16:41", '%Y-%m-%d_%H:%M'))
=> 1189521660.0
FWIW, you may also want to have a look at the datetime module, with
special attention to the timedelta class.
HTH
More information about the Python-list
mailing list