js将秒转换成几天几小时几分几秒,每秒刷新

文章正文
发布时间:2025-05-24 05:58

//js将秒转换成几天几小时几分几秒 function getDuration(second) { var days = Math.floor(second / 86400); var hours = Math.floor((second % 86400) / 3600); var minutes = Math.floor(((second % 86400) % 3600) / 60); var seconds = Math.floor(((second % 86400) % 3600) % 60); var duration = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒"; return duration; } //每秒刷新(秒杀) secondToDate(msd){ var that = this; var interval = setInterval(function () { //msd为0或者空时,无秒杀 if (null != msd && "" != msd && 0 != msd) { if (msd > 60 && msd < 60 * 60) { that.day = '00'; that.h = '00'; var m1 = Math.floor(((msd % 86400) % 3600) / 60).toString(); var s2 = Math.floor(((msd % 86400) % 3600) % 60).toString(); that.m = m1.length < 2 ? '0' + m1 : m1; that.s = s1.length < 2 ? '0' + s1 : s1; }else if(msd >= 60 * 60 && msd < 60 * 60 * 24){ that.day = '00'; var h1 = Math.floor((msd % 86400) / 3600).toString(); var m1 = Math.floor(((msd % 86400) % 3600) / 60).toString(); var s1 = Math.floor(((msd % 86400) % 3600) % 60).toString(); that.h = h1.length < 2 ? '0' + h1 : h1; that.m = m1.length < 2 ? '0' + m1 : m1; that.s = s1.length < 2 ? '0' + s1 : s1; }else if(msd >= 60 * 60 * 24){ var day1 = Math.floor(msd / 86400).toString(); var h1 = Math.floor((msd % 86400) / 3600).toString(); var m1 = Math.floor(((msd % 86400) % 3600) / 60).toString(); var s1 = Math.floor(((msd % 86400) % 3600) % 60).toString(); that.day = day1.length < 2 ? '0' + day1 : day1; that.h = h1.length < 2 ? '0' + h1 : h1; that.m = m1.length < 2 ? '0' + m1 : m1; that.s = s1.length < 2 ? '0' + s1 : s1; }else{ that.day = '00'; that.h = '00'; that.m = '00'; var s1 = Math.floor(((msd % 86400) % 3600) % 60).toString(); that.s = s1.length < 2 ? '0' + s1 : s1; } //开启秒杀 that.seckillState = '1'; msd--; }else{ //不存在秒杀 that.seckillState = '0'; //关闭定时器 clearInterval(interval); } }, 1000) },