[Answer]-Django deltatime formatting

1👍

✅

This should be solvable by a few simple string methods (assuming your input is indeed a string).

td = "0:01:23.415000"
time = td.split(':')
time_string = ''
if int(time[0]):
    time_string += time[0]
    if int(time[0]) > 1:
        time_string += " hours, "
    else:
        time_string += " hour, "
if int(time[1]):
    time_string += str(time[1])
    if int(time[1]) > 1:
        time_string += " minute, "
    else:
        time_string += " minutes, "
if float(time[2]):
    time_string += str(int(float(time[3])))
    if int(float(time[2])) > 1:
        time_string += " second"
    else:
        time_string += " seconds"
print time_string

This is mindful of plurality of units, and rounding on the seconds. There is probably a more concise way to write this, but I’ll leave that as an exercise for the reader 😉

Leave a comment