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

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)+也可以)。

其他回答

要添加前导零,我只需这样做:

const secondstom昆虫填充=时间=> { const minutes = "0" + Math。楼层(时间/ 60); Const seconds = "0" +(时间-分钟* 60); 返回minutes.substr(-2) + ":" + seconds.substr(-2); }; console.log (secondsToMinSecPadded (241));

很好,很短

export function TrainingTime(props) {
    const {train_time } = props;
    const hours = Math.floor(train_time/3600);
    const minutes = Math.floor((train_time-hours * 3600) / 60);
    const seconds = Math.floor((train_time%60));

    return `${hours} hrs  ${minutes} min  ${seconds} 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

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 */

Var秒= 60; var measuredTime =新的日期(空); measuredTime.setSeconds(秒);//指定SECONDS的值 var Time = measuredTime.toISOString()。8 substr(11日); . getelementbyid (id1”)。value =时间; < div class = "形式的班级”> <label for="course" class="col-md-4">Time</label> . < div class = " col-md-8”> <input type="text" class="form-control" id="id1" name="field">最小值 < / div > < / div >