JS格式化倒计时秒杀的函数【距XXX开始时间:xx天xx小时xx分xx秒】
惯例
直接上代码:
显示诸如:【距XXX开始时间:xx天xx小时xx分xx秒】
function format_time(time) { var day, hour, minute, second, str; if (time > 86400) { day = Math.floor(time / 86400); hour = Math.floor((time - day * 86400) / 3600); minute = Math.floor((time - day * 86400 - hour * 3600) / 60); second = Math.floor(time - day * 86400 - hour * 3600 - minute * 60); if (hour < 10) hour = "0" + hour; if (minute < 10) minute = "0" + minute; if (second < 10) second = "0" + second; str = day + "天" + hour + "小时" + minute + "分" + second + "秒"; } else if (time >= 3600) { hour = Math.floor(time / 3600); minute = Math.floor((time - hour * 3600) / 60); second = Math.floor(time - hour * 3600 - minute * 60); if (hour < 10) hour = "0" + hour; if (minute < 10) minute = "0" + minute; if (second < 10) second = "0" + second; str = hour + "小时" + minute + "分" + second + "秒"; } else if (time >= 60) { minute = Math.floor(time / 60); second = Math.floor(time - minute * 60); if (minute < 10) minute = "0" + minute; if (second < 10) second = "0" + second; str = minute + "分" + second + "秒"; } else { str = time + "秒"; } return str; }