Javascript seconds to minutes and seconds -
this common problem i'm not sure how solve it. code below works fine.
var mind = time % (60 * 60); var minutes = math.floor(mind / 60); var secd = mind % 60; var seconds = math.ceil(secd);
however, when 1 hour or 3600 seconds returns 0 minutes , 0 seconds. how can avoid returns minutes?
thanks
you’re doing wrong. number of full minutes, divide number of total seconds 60 (60 seconds/minute):
var minutes = math.floor(time / 60);
and remaining seconds, multiply full minutes 60 , subtract total seconds:
var seconds = time - minutes * 60;
now if want full hours too, divide number of total seconds 3600 (60 minutes/hour · 60 seconds/minute) first, calculate remaining seconds:
var hours = math.floor(time / 3600); time = time - hours * 3600;
then calculate full minutes , remaining seconds.
bonus:
use following code pretty-print time (suggested dru)
function str_pad_left(string,pad,length) { return (new array(length+1).join(pad)+string).slice(-length); } var finaltime = str_pad_left(minutes,'0',2)+':'+str_pad_left(seconds,'0',2);
Comments
Post a Comment