Convert Seconds to formatted time

Here we have a function that allows us to convert time in second (as a decimal number) into a human-friendly time format. By default the format is HH:mm:ss,msmsms

You can use the function by simply pasting the code below into your calculator, no other function is required to use sec2time.

The functions and its parameters

  • Input: timeInSeconds must be a variable of type number representing the second elapsed.

  • Output: The functions returns a string with format HH:mm:ss,msmsms. For 1-digit values a zero is added at the begining.

Here is the javascript code of the function, ready to be copied and pasted in your calculator:

function sec2time(timeInSeconds) {
var pad = function(num, size) { return ('000' + num).slice(size * -1); },
var time = parseFloat(timeInSeconds).toFixed(3),
var hours = Math.floor(time / 60 / 60),
var minutes = Math.floor(time / 60) % 60,
var seconds = Math.floor(time - minutes * 60),
var milliseconds = time.slice(-3);

  return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2) + ',' + pad(milliseconds, 3);
}

Note

You can find the function on GitHub as: sec2time