8

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;
   }
Bo Persson
  • 88,437
  • 31
  • 141
  • 199
sammyukavi
  • 1,471
  • 2
  • 23
  • 48

1 Answers1

42

this should work:

var time=('0'  + minutes).slice(-2)+':'+('0' + seconds).slice(-2);
Ankur
  • 12,090
  • 6
  • 34
  • 67