How to convert a timedelta object to a string?

skip at pobox.com skip at pobox.com
Thu Sep 14 18:19:08 EDT 2006


    >>> startTime = datetime.timedelta(seconds=45,minutes=22,hours=10)
    >>> stopTime = datetime.timedelta(seconds=25,minutes=2,hours=4)
    >>> delta = startTime-stopTime
    >>> time.strftime("%H:%M:%S",time.gmtime(delta.seconds))
    '06:20:20'

Which, alas, will lose any subsecond resolution:

    >>> str(datetime.timedelta(seconds=0.1))
    '0:00:00.100000'

Skip



More information about the Python-list mailing list