How to get millisec/fractional seconds out of a time object ?

MRAB google at mrabarnett.plus.com
Tue Jan 6 16:49:58 EST 2009


David at bag.python.org wrote:
> Thanks for help to a beginner.
> 
> script23
>         import time
>         import datetime
>         start_time = datetime.datetime.now()
>         time.sleep(0.14)
>         end_time = datetime.datetime.now()
>         datetime.timedelta = end_time - start_time

You've just overwritten the timedelta class in the datetime module! Do 
something like this instead:

     elapsed_time = end_time - start_time

>         print(datetime.timedelta)    #  works, prints 0:00:0.141000
>         print(datetime.timedelta.seconds)    #  prints 0
>         print(datetime.timedelta.milliseconds)  # fails 
>                               < object has no attribute milliseconds >
> 
     print (elapsed_time.microseconds / 1.0E6)

>    How do I get the 0.141000 out of that or any time object ?
>    On line docs are arcane to a novice.
> 




More information about the Python-list mailing list