js秒转时分秒,小于十补零

在网上找了很多都很复杂,看的晕头转向的,不过找到了简介版的,先记录下

 

var s = 23886;   //需要转的秒数
var m;

setInterval(function(){
    m = secondToDate(s)
    console.log(m)
    s--;
},1000)


// 输出03:05:59  时分秒
function secondToDate(result) {
    var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
    var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
    var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
    return result = h + ":" + m + ":" + s;
}

 

输出

 

posted @ 2017-11-02 10:19  深以为然  阅读(12532)  评论(1编辑  收藏  举报