js-把秒转化为 *天*小时*分钟*秒

 方法:

function formatSecToStr(seconds){
    let daySec = 24 *  60 * 60;
	let hourSec=  60 * 60;
	let minuteSec=60;
    let dd = Math.floor(seconds / daySec);
    let hh = Math.floor((seconds % daySec) / hourSec);
    let mm = Math.floor((seconds % hourSec) / minuteSec);
	let ss=seconds%minuteSec;
    if(dd > 0){
      return dd + "天" + hh + "小时" + mm + "分钟"+ss+"秒";
    }else if(hh > 0){
      return hh + "小时" + mm + "分钟"+ss+"秒";
    } else if (mm > 0){
      return mm + "分钟"+ss+"秒";
    }else{
      return ss+"秒";
    }
}

结果:

js-把秒转化为 *天*小时*分钟*秒_第1张图片

你可能感兴趣的:(JS)