这是一个常见的问题,但我不知道如何解决它。下面的代码可以正常工作。
var mind = time % (60 * 60);
var minutes = Math.floor(mind / 60);
var secd = mind % 60;
var seconds = Math.ceil(secd);
然而,当我到达1小时或3600秒时,它返回0分和0秒。我如何避免这种情况,让它返回所有的分钟?
这是一个常见的问题,但我不知道如何解决它。下面的代码可以正常工作。
var mind = time % (60 * 60);
var minutes = Math.floor(mind / 60);
var secd = mind % 60;
var seconds = Math.ceil(secd);
然而,当我到达1小时或3600秒时,它返回0分和0秒。我如何避免这种情况,让它返回所有的分钟?
当前回答
下面的函数将帮助您获得日,小时,分钟,秒
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));
其他回答
function formatSeconds(s: number) {
let minutes = ~~(s / 60);
let seconds = ~~(s % 60);
return minutes + ':' + seconds;
}
另一个奇妙的解决方案:
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的缩写。楼层,请参阅此链接以获取更多信息
对于那些希望能快速简单地将秒格式化为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)+也可以)。
要得到完整的分钟数,将总秒数除以60(60秒/分钟):
const minutes = Math.floor(time / 60);
为了得到剩余的秒数,将整个分钟数乘以60,然后减去总秒数:
const seconds = time - minutes * 60;
现在,如果你也想得到完整的小时数,首先用总秒数除以3600(60分钟/小时·60秒/分钟),然后计算剩余的秒数:
const hours = Math.floor(time / 3600);
time = time - hours * 3600;
然后计算完整的分钟数和剩余的秒数。
奖金:
使用以下代码来漂亮地打印时间(由Dru建议):
function str_pad_left(string, pad, length) {
return (new Array(length + 1).join(pad) + string).slice(-length);
}
const finalTime = str_pad_left(minutes, '0', 2) + ':' + str_pad_left(seconds, '0', 2);
1 -得到剩余的除法使用%。现在你有了一分钟还不够完整的几秒钟 2 -从总数中减去步骤1中获得的秒数。现在你有会议记录
例如,让我们假设你有700秒:
seconds = 700%60); //40 seconds
minutes = (700 - (700%60))/60; //11
//11:40