/ Published in: JavaScript
URL: http://www.misteroneill.com
I wrote this for displaying audio/video clip durations. This function takes a single argument (a number of seconds) and returns a formatted string in the format "hh:mm:ss". Hours will not show if the total is less than an hour, minutes will always show. Minutes will show leading zero only if the total is greater than an hour. If less than a minute, will show "0" for minutes. Examples: 1:15:05 (4505 seconds), 36:01 (2161 seconds), 0:45 (45 seconds)
Expand |
Embed | Plain Text
function secondsToHms(d) { d = Number(d); var h = Math.floor(d / 3600); var m = Math.floor(d % 3600 / 60); var s = Math.floor(d % 3600 % 60); return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s); }
You need to login to post a comment.
