Python datetime – Convert seconds to hh:mm:ss

In this post we will be creating a Python script that will convert any number of seconds to a ‘hh:mm:ss’ format, to do this we will be using the datetime module which supplies classes for manipulating dates and times. In particular we will be utilizing timedelta objects which represent a duration of time.

In this example we will be converting 1673 seconds in to the ‘hh:mm:ss’ format, see the snippet of code below for how this is done.

import datetime

Format= str(datetime.timedelta(seconds=1673))
print(Format)

The above would output the following as output.

0:27:53
>>> 

Leave a Reply