javascript - Converting milliseconds while detecting single digit integers -
i created function detects , converts milliseconds minutes , seconds. i'd prepend 0 on either (or both) minute , second variables if number comes out less ten. instance if had integer 184213
evaluates 3:4
i'd 03:04
. there short , concise way without having write out long conditional or ternary?
mstotime(184213); function mstotime(milliseconds) { var minutes = parseint(milliseconds/(1000*60)%60), seconds = parseint((milliseconds/1000)%60); return minutes + ':' + seconds; }
a different approach utilizing date
, slice
:
function mstotime(milliseconds) { var date = new date(milliseconds); return date.totimestring().slice(3,8); } mstotime(184213); // outputs: "03:04"
a small caveat of course have limit of "23:59" , floor value milliseconds value on minutes not shown.