Unlike the datetime and other date related objects in python, timedelta does not have any useful strong formatting options.
It also has no external method that can format it into something that you might want to present to an end user. As such, say you wanted Hours:Minutes:Seconds, you’d have to do some fancy things with the .seconds attribute on the timedelta.
To save time, the code for ‘fancy things’ is given below:
hours, remainder = divmod(duration_time_delta, 3600) minutes, seconds = divmod(remainder, 60) duration_formatted = '%s:%s:%s' % (hours, minutes, seconds)
Consider ‘%d:%02d:%02d’ % (hours, minutes, seconds) instead.
1:2:3 will look weird.
str(time_delta)
@Marius.
That’s much better formatting, thanks!
@anon
Yes, that will work, however the output format isn’t exactly the most human friendly, I don’t really need to know it down to the millisecond and below, it’s not particularly clean to read in a user interface.