How can I display time with leading zeros?

I created a time function to calculate media playback time. It does not display any leading zeros. How can I 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; } 
+6
source share
1 answer

this should work:

 var time=('0' + minutes).slice(-2)+':'+('0' + seconds).slice(-2); 
+31
source

Source: https://habr.com/ru/post/924355/


All Articles