这是一个常见的问题,但我不知道如何解决它。下面的代码可以正常工作。

var mind = time % (60 * 60);
var minutes = Math.floor(mind / 60);
         
var secd = mind % 60;
var seconds = Math.ceil(secd);

然而,当我到达1小时或3600秒时,它返回0分和0秒。我如何避免这种情况,让它返回所有的分钟?


当前回答

试试这个: 将秒转换为小时,分钟和秒。

function convertTime(sec) {
    var hours = Math.floor(sec/3600);
    (hours >= 1) ? sec = sec - (hours*3600) : hours = '00';
    var min = Math.floor(sec/60);
    (min >= 1) ? sec = sec - (min*60) : min = '00';
    (sec < 1) ? sec='00' : void 0;

    (min.toString().length == 1) ? min = '0'+min : void 0;    
    (sec.toString().length == 1) ? sec = '0'+sec : void 0;    
    
    return hours+':'+min+':'+sec;
}

其他回答

我更喜欢将Millisecond视为自己的单元,而不是其他东西的子单元。从这个意义上说,它的值是0-999,所以你会想要填充3而不是2,就像我在其他答案中看到的那样。下面是一个实现:

函数格式(n) { 让mil_s =字符串(n % 1000)。padStart (3 ' 0 '); n =数学。Trunc (n / 1000); let sec_s =字符串(n % 60)。padStart (2, ' 0 '); n =数学。Trunc (n / 60); 返回String(n) + ' m ' + sec_s + ' s' + mil_s + ' ms'; } console.log(格式(241));

https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/String/padStart

使用ES6清洁一个衬垫


const secondsToMinutes = seconds => Math.floor(seconds / 60) + ':' + ('0' + Math.floor(seconds % 60)).slice(-2);

另一个奇妙的解决方案:

function fancyTimeFormat(duration) { // Hours, minutes and seconds const hrs = ~~(duration / 3600); const mins = ~~((duration % 3600) / 60); const secs = ~~duration % 60; // Output like "1:01" or "4:03:59" or "123:03:59" let ret = ""; if (hrs > 0) { ret += "" + hrs + ":" + (mins < 10 ? "0" : ""); } ret += "" + mins + ":" + (secs < 10 ? "0" : ""); ret += "" + secs; return ret; } console.log( fancyTimeFormat(1), fancyTimeFormat(10), fancyTimeFormat(100), fancyTimeFormat(1000), fancyTimeFormat(10000), );

~~是Math的缩写。楼层,请参阅此链接以获取更多信息

你也可以使用本机Date对象:

var date = new Date(null);
date.setSeconds(timeInSeconds);

// retrieve time ignoring the browser timezone - returns hh:mm:ss
var utc = date.toUTCString();
// negative start index in substr does not work in IE 8 and earlier
var time = utc.substr(utc.indexOf(':') - 2, 8)

// retrieve each value individually - returns h:m:s
var time = date.getUTCHours() + ':' + date.getUTCMinutes() + ':' +  date.getUTCSeconds();

// does not work in IE8 and below - returns hh:mm:ss
var time = date.toISOString().substr(11, 8);

// not recommended - only if seconds number includes timezone difference
var time = date.toTimeString().substr(0, 8);

当然,这个解决方案只适用于小于24小时的timeInSeconds;)

strftime.js (strftime github)是最好的时间格式库之一。它非常轻——30KB——而且非常有效。使用它,您可以在一行代码中轻松地将秒转换为时间,主要依赖于本机Date类。

当创建一个新的Date时,每个可选参数的位置如下:

new Date(year, month, day, hours, minutes, seconds, milliseconds);

因此,如果你初始化一个new Date,所有参数都为0,直到秒,你会得到:

var seconds = 150;
var date = new Date(0,0,0,0,0,seconds);
=> Sun Dec 31 1899 00:02:30 GMT-0500 (EST)

可以看到150秒是2分钟和30秒,如创建的日期所示。然后使用strftime格式(“MM:SS”为“%M:%S”),它将输出您的分钟字符串。

var mm_ss_str = strftime("%M:%S", date);
=> "02:30"

在一行中,它看起来像这样:

var mm_ss_str = strftime('%M:%S', new Date(0,0,0,0,0,seconds));
=> "02:30"

另外,这将允许您根据秒数交换支持HH:MM:SS和MM:SS。例如:

# Less than an Hour (seconds < 3600)
var seconds = 2435;
strftime((seconds >= 3600 ? '%H:%M:%S' : '%M:%S'), new Date(0,0,0,0,0,seconds));
=> "40:35"

# More than an Hour (seconds >= 3600)
var seconds = 10050;
strftime((seconds >= 3600 ? '%H:%M:%S' : '%M:%S'), new Date(0,0,0,0,0,seconds));
=> "02:47:30"

当然,如果您希望时间字符串或多或少具有语义,您可以简单地将您想要的任何格式传递给strftime。

var format = 'Honey, you said you\'d be read in %S seconds %M minutes ago!';
strftime(format, new Date(0,0,0,0,0,1210));
=> "Honey, you said you'd be read in 10 seconds 20 minutes ago!"