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

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

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


当前回答

对于那些希望能快速简单地将秒格式化为M:SS的人来说:

function fmtMSS(s){return(s-(s%=60))/60+(9<s?':':':0')+s}

做. . 该函数接受一个数字(首选)或一个字符串(2转换'惩罚',你可以通过在函数调用的参数s中前置+来减半,如:fmtMSS(+strSeconds)),表示正整数秒s作为参数。

例子:

fmtMSS(    0 );  //   0:00
fmtMSS(   '8');  //   0:08
fmtMSS(    9 );  //   0:09
fmtMSS(  '10');  //   0:10
fmtMSS(   59 );  //   0:59
fmtMSS( +'60');  //   1:00
fmtMSS(   69 );  //   1:09
fmtMSS( 3599 );  //  59:59
fmtMSS('3600');  //  60:00
fmtMSS('3661');  //  61:01
fmtMSS( 7425 );  // 123:45

分解:

function fmtMSS(s){   // accepts seconds as Number or String. Returns m:ss
  return( s -         // take value s and subtract (will try to convert String to Number)
          ( s %= 60 ) // the new value of s, now holding the remainder of s divided by 60 
                      // (will also try to convert String to Number)
        ) / 60 + (    // and divide the resulting Number by 60 
                      // (can never result in a fractional value = no need for rounding)
                      // to which we concatenate a String (converts the Number to String)
                      // who's reference is chosen by the conditional operator:
          9 < s       // if    seconds is larger than 9
          ? ':'       // then  we don't need to prepend a zero
          : ':0'      // else  we do need to prepend a zero
        ) + s ;       // and we add Number s to the string (converting it to String as well)
}

注意:负数范围可以通过在返回表达式前加上(0>s?(s=-s,'-'): ")+来增加(实际上,(0>s?(s=-s,'-'):0)+也可以)。

其他回答

function secondsToMinutes(time){
    return Math.floor(time / 60)+':'+Math.floor(time % 60);
}

秒到h:mm:ss

var hours = Math.floor(time / 3600);
time -= hours * 3600;

var minutes = Math.floor(time / 60);
time -= minutes * 60;

var seconds = parseInt(time % 60, 10);

console.log(hours + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds));

2020年更新

使用基本的数学和简单的javascript,这可以在几行代码中完成。

将7735秒转换为HH:MM:SS。


数学:

计算使用:

Math.floor() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

Math.floor()函数的作用是:返回小于或等于给定数字的最大整数。

%算术运算符(余数)- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder

余数操作符返回一个操作数除第二个操作数时的余数。它总是带红利的符号。

查看下面的代码。秒除以3600得到小时数和余数,余数用于计算分数和秒数。

HOURS => 7735 / 3600 = 2余535

MINUTES => 535 / 60 = 8余55

秒=> 55


前导零:

这里的许多答案使用复杂的方法来正确地显示小时、分钟和秒数,前导0 - 45、04等。这可以使用padStart()来完成。这适用于字符串,因此必须使用toString()将数字转换为字符串。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

padStart()方法用另一个字符串填充当前字符串(如果需要多次),直到生成的字符串达到给定的长度。填充从当前字符串的开始处应用。


代码:

function secondsToTime(e){ const h = Math.floor(e / 3600).toString().padStart(2,'0'), m = Math.floor(e % 3600 / 60).toString().padStart(2,'0'), s = Math.floor(e % 60).toString().padStart(2,'0'); return h + ':' + m + ':' + s; //return `${h}:${m}:${s}`; } console.log(secondsToTime(7735)); // 02:08:55 /* secondsToTime(SECONDS) // HH:MM:SS secondsToTime(8) // 00:00:08 secondsToTime(68) // 00:01:08 secondsToTime(1768) // 00:29:28 secondsToTime(3600) // 01:00:00 secondsToTime(5296) // 01:28:16 secondsToTime(7735) // 02:08:55 secondsToTime(45296) // 12:34:56 secondsToTime(145296) // 40:21:36 secondsToTime(1145296) // 318:08:16 */

下面的函数将帮助您获得日,小时,分钟,秒

toDDHHMMSS(inputSeconds){
        const Days = Math.floor( inputSeconds / (60 * 60 * 24) );
        const Hour = Math.floor((inputSeconds % (60 * 60 * 24)) / (60 * 60));
        const Minutes = Math.floor(((inputSeconds % (60 * 60 * 24)) % (60 * 60)) / 60 );
        const Seconds = Math.floor(((inputSeconds % (60 * 60 * 24)) % (60 * 60)) % 60 );
        let ddhhmmss  = '';
        if (Days > 0){
            ddhhmmss += Days + ' Day ';
        }
        if (Hour > 0){
            ddhhmmss += Hour + ' Hour ';
        }

        if (Minutes > 0){
            ddhhmmss += Minutes + ' Minutes ';
        }

        if (Seconds > 0){
            ddhhmmss += Seconds + ' Seconds ';
        }
        return ddhhmmss;
    }
alert( toDDHHMMSS(2000));

Moment.js

如果你使用的是Moment.js,那么你可以使用内置的Duration对象

const duration = moment.duration(4825, 'seconds');

const h = duration.hours(); // 1
const m = duration.minutes(); // 20
const s = duration.seconds(); // 25