标签:amp javascrip gettime eth second timer get document 需求
需求:获得一天5个不同阶段的倒计时,为期10天。
计数时段分比为:
第一时段9:00-11:59、第二时段12:00-17:59、第三时段18:00-20:59、第四时段21:00-23:59、第五时段次日0:00-8:59,共五个时段。
简单的写了如下:
DOM:
<div id="txt"></div>
JS:
//分时段倒计时
function ShowCountDown() {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth(),
day = now.getDate(),
h = now.getHours(),
endDate;
if (h >= 0 && h < 9) {
endDate = new Date(year, month, day, 8, 59, 59);
} else if (h >= 9 && h < 12) {
endDate = new Date(year, month, day, 11, 59, 59);
} else if (h >= 12 && h < 18) {
endDate = new Date(year, month, day, 17, 59, 59);
} else if (h >= 18 && h < 21) {
endDate = new Date(year, month, day, 20, 59, 59);
} else {
endDate = new Date(year, month, day, 23, 59, 59);
}
var leftsecond = parseInt((endDate.getTime() - now.getTime()) / 1000);
var hour = Math.floor(leftsecond / 3600);
var minute = Math.floor((leftsecond - hour * 3600) / 60);
var second = Math.floor(leftsecond - hour * 3600 - minute * 60);
document.getElementById(‘txt‘).innerHTML = "距离此时段结束还有:" + hour + "小时" + minute + "分" + second + "秒";
countDowntimer = setTimeout(function() {
ShowCountDown();
}, 1000);
}
ShowCountDown();
标签:amp javascrip gettime eth second timer get document 需求
原文地址:http://www.cnblogs.com/leaf930814/p/6491954.html