Skip to content

Instantly share code, notes, and snippets.

@farkmarnum
Forked from vankasteelj/sec2time.js
Last active July 31, 2019 23:06
Show Gist options
  • Save farkmarnum/32b43433cf75d1ed77a65301f5fdb6c6 to your computer and use it in GitHub Desktop.
Save farkmarnum/32b43433cf75d1ed77a65301f5fdb6c6 to your computer and use it in GitHub Desktop.
Javascript - Seconds to Time (hh:mm:ss,ms) -> sec2time(593.685038) becomes 00:09:53,685
function sec2time(timeInSeconds) {
var pad = function(num, size) { return ('000' + num).slice(size * -1); },
time = parseFloat(timeInSeconds).toFixed(3),
hours = Math.floor(time / 60 / 60),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60 - hours * 60 * 60),
milliseconds = time.slice(-3);
return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2) + ',' + pad(milliseconds, 3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment