JavaScriptカウントダウン

1770 ワード

現在の時刻
//       
function getTime() {             //        
    var currentDate = new Date();
    var year = currentDate.getFullYear();
    var month = currentDate.getMonth() + 1;
    var date = currentDate.getDate();
    var day = currentDate.getDay();
    var hour = checkTime(currentDate.getHours());
    var minute = checkTime(currentDate.getMinutes());
    var seconds = checkTime(currentDate.getSeconds());
    var weekday = [
        "   ",
        "   ",
        "   ",
        "   ",
        "   ",
        "   ",
        "   "
    ];
    document.getElementById("today").innerHTML = year + " " + month + " " + date + " " + "  " + weekday[day] +
            hour + ":" + minute + ":" + seconds;
    setTimeout(getTime, 10);

}
カウントダウンを表示
//       
function setTime() {
    var currentTime = new Date();
    var deadTime = new Date(2020, 5, 6);
    var leftTime = parseInt((deadTime - currentTime) / 1000);
    var days = parseInt(leftTime / (24 * 60 * 60));
    var hours = checkTime(parseInt(leftTime / (60 * 60) % 24));
    var minutes = checkTime(parseInt(leftTime / 60 % 60));
    var seconds = checkTime(parseInt(leftTime % 60));
    document.getElementById("left-time").innerHTML = " 2020 6 6   : " + days + " " + hours + "  " +
            minutes + " " + seconds + " ";

    if (leftTime <= 0) {
        document.getElementById("left-time").innerHTML = "     ";
    }
    setTimeout(setTime, 10);

}

function checkTime(time) {    //     ,    
    if (time < 10) {
        time = "0" + time;
    }
    return time;
}