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

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 formatSeconds(s: number) {
    let minutes = ~~(s / 60);
    let seconds = ~~(s % 60);
    return minutes + ':' + seconds;
  }

其他回答

您已经编写了足够多的代码来跟踪时间的分钟和秒部分。

你可以把时间因素加进去:

var hrd = time % (60 * 60 * 60);
var hours = Math.floor(hrd / 60);

var mind = hrd % 60;
var minutes = Math.floor(mind / 60);

var secd = mind % 60;
var seconds = Math.ceil(secd);

var moreminutes = minutes + hours * 60

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

对于那些希望能快速简单地将秒格式化为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 convertSecondsToMinutesAndSeconds(seconds){
            var minutes;
            var seconds;
            minutes = Math.floor(seconds/60);
            seconds = seconds%60;

            return [minutes, seconds];
        }

所以这个:

var minutesAndSeconds = convertSecondsToMinutesAndSeconds(101);

将有以下输出:

[1,41];

然后你可以像这样打印它:

console.log('TIME : ' +  minutesSeconds[0] + ' minutes, ' + minutesSeconds[1] + ' seconds');

//TIME : 1 minutes, 41 seconds