如何用前导零显示时间

How can I display time with leading zeros?

本文关键字:显示 时间 何用前      更新时间:2023-09-26

我创建了一个时间函数来计算媒体播放时的时间。它不显示任何前导零。如何使其显示任何前导零?

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;
   }

这应该有效:

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