如何使用javascript将秒转换为分钟.

how to convert seconds to minutes using javascript...?

本文关键字:转换 分钟 何使用 javascript      更新时间:2023-09-26

我正在制作一个在线测试系统,我想将计时器从秒转换为分秒。请帮我解决这个问题这里是我的代码

<div id="divCounter"></div>
<script type="text/javascript">
    if(localStorage.getItem("counter")){
        if(localStorage.getItem("counter") <= 0){
            var value = 110;
        }
     else{  
         var value = localStorage.getItem("counter");
     }
}
else{
    var value = 10;
}
var counter = function (){
document.getElementById('divCounter').innerHTML = localStorage.getItem("counter");
if(value <= 0){
        window.location="http://www.google.com"
    }else{
       value = parseInt(value)-1;
       localStorage.setItem("counter", value);
    }
};
var interval = setInterval(function (){counter(value);}, 1000);

试试这样的东西:

function convert(value) {
    return Math.floor(value / 60) + ":" + (value % 60 ? value % 60 : '00')
}

DEMO

value/60+":"+value%60,格式为(m)m:ss找出正确的填充

我建议您简单地使用这个函数(从这里开始),它将秒数转换为表示小时、分钟和秒的字符串,格式为HH:MM:SS:

function secondsToTimeString(seconds) {
    var minutes = 0, hours = 0;
    if (seconds / 60 > 0) {
        minutes = parseInt(seconds / 60, 10);
        seconds = seconds % 60;
    }
    if (minutes / 60 > 0) {
        hours = parseInt(minutes / 60, 10);
        minutes = minutes % 60;
    }
    return ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2);
}