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

Chris Rebert clp2 at rebertia.com
Tue Jan 6 22:46:46 CET 2009


On Tue, Jan 6, 2009 at 1:34 PM,  <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
>        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 >
>
>   How do I get the 0.141000 out of that or any time object ?
>   On line docs are arcane to a novice.

Not all that arcane.

>From http://docs.python.org/library/datetime.html :
"""
timedelta Objects

A timedelta object represents a duration, the difference between two
dates or times.

[...]

Attribute 	Value
days 	Between -999999999 and 999999999 inclusive
seconds 	Between 0 and 86399 inclusive
microseconds 	Between 0 and 999999 inclusive
"""

The attribute you're looking for is 'microseconds', not 'milliseconds'.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list