Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display time with leading zeros? [duplicate]

I have created a time function to calculate time as media is playing. It does not display any leading zeros. How can I make it display any leading zeros?

function converttoTime(timeInSeconds)
   {
    var minutes = Math.round(Math.floor(timeInSeconds / 60));
    var seconds = Math.round(timeInSeconds - minutes * 60);
    //var hours = Math.round(Math.floor(timeInSeconds / 3600));
    //time = time - hours * 3600;
    var time=minutes+':'+seconds;
    return time;
   }
like image 838
sammyukavi Avatar asked Sep 01 '12 18:09

sammyukavi


1 Answers

this should work:

var time=('0'  + minutes).slice(-2)+':'+('0' + seconds).slice(-2);
like image 149
Ankur Avatar answered Oct 31 '22 16:10

Ankur