当前时间和倒计时效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>倒计时效果</title>
<body>
当前时间:<p id="p1"></p>
限时抢购:<p id="p2"></p>
</body>
    <script type="text/javascript">
        function checkTime(i) {
            if (i<10) {
                i="0"+i;
            }
            return i;
        }
        window.onload=function () {

            var p1=document.getElementById("p1"),
                p2=document.getElementById("p2");
            showtime1();
            showtime2();
           
        }
        function showtime1() {
            var nowdate=new Date();
            var year=nowdate.getFullYear(),//年份
                month=nowdate.getMonth()+1,//月份
                date=nowdate.getDate(),//
                week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
                day=nowdate.getDay(),//getDay获取0-6
                h=nowdate.getHours(),
                h=checkTime(h),
                m=nowdate.getMinutes(),
                m=checkTime(m),
                s=nowdate.getSeconds(),
                s=checkTime(s);
            p1.innerHTML=year+""+month+""+date+""+week[day]+h+""+m+""+s;
            setTimeout(showtime1, 1000);
        }
 function showtime2() {
            var nowtime=new Date(),
                endtime=new Date("2020/1/1,00:00:00"),
                lefttime=parseInt((endtime.getTime()-nowtime.getTime())/1000),
                d=Math.floor(lefttime/(60*60*24)),
                h=Math.floor(lefttime/(60*60)%24),
                m=Math.floor(lefttime/60%60),
                s=Math.floor(lefttime%60);
            p3.innerHTML=d+""+h+"小时"+m+""+s+"";
            setTimeout(showtime3, 1000);
        }
    </script>
</head>

</html>
原文地址:https://www.cnblogs.com/yourName/p/9408433.html