Elapsed time in human readable format in Python

datetime timedelta

It is rather easy to get the elapsed time in seconds or miliseconds, but if the we are trying to measure a very long running process we will probably want to see the elapsed time in a more human readable format using minutes and even hours. We can convert seconds into such format using the datetime.timedelta function.

examples/elapsed_time_in_human_readable_form.py

import datetime

def main():
    print(datetime.timedelta(seconds = 1.429948091506958))
    print(datetime.timedelta(seconds = 1))
    print(datetime.timedelta(seconds = 5))
    print(datetime.timedelta(seconds = 61))
    print(datetime.timedelta(seconds = 100))
    print(datetime.timedelta(seconds = 1000))
    print(datetime.timedelta(seconds = 10000))

main()

0:00:01.429948
0:00:01
0:00:05
0:01:01
0:01:40
0:16:40
2:46:40

Related Pages

Elapsed time in seconds in Python

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Python Maven web site.

Gabor Szabo