这是一个常见的问题,但我不知道如何解决它。下面的代码可以正常工作。
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秒。我如何避免这种情况,让它返回所有的分钟?
当前回答
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!"
其他回答
说说我的意见:
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
对于那些希望能快速简单地将秒格式化为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)+也可以)。
秒到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));
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 >
试试这个: 将秒转换为小时,分钟和秒。
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;
}