js倒计时 天,时,分,秒

js倒计时 天,时,分,秒

<!doctype html>  
<html>  

    <head>  
        <meta charset="UTF-8">  
        <title>js倒计时</title>  
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
    </head>  

    <body> 
		
		<body>
			<div id="time"></div>
		</body>
		
		<script type="text/javascript" charset="utf-8">
				
				function count_down(time){
					//延迟一秒执行自己
					var iCount = setInterval(function () {
						//结束时间
						var endDate = new Date(time);
						//当前时间
						var nowDate = new Date();
						//相差的总秒数
						var totalSeconds = parseInt((endDate - nowDate) / 1000);
						
						//关闭定时器
						if(totalSeconds<=0) {
							clearInterval(iCount);
							alert("执行完毕");
							return;
						}
						
						//天数
						var days = Math.floor(totalSeconds / (60 * 60 * 24));
						//取模(余数)
						var modulo = totalSeconds % (60 * 60 * 24);
						//小时数
						var hours = Math.floor(modulo / (60 * 60));
						modulo = modulo % (60 * 60);
						//分钟
						var minutes = Math.floor(modulo / 60);
						//秒
						var seconds = modulo % 60;
						
						var t = days<10?'0'+days.toString():days;
						var s = hours<10?'0'+hours.toString():hours;
						var f = minutes<10?'0'+minutes.toString():minutes;
						var m = seconds<10?'0'+seconds.toString():seconds;
						
						var remaining_time = "还剩:" + t + "天" + s + "小时" + f + "分钟" + m + "秒";
						document.getElementById("time").innerHTML = remaining_time;
					}, 1000)
				}
				
				count_down("2019-07-01 12:00:00");
		</script>
    </body>  

</html>  
上一篇:js获取当前时间:yyyy-MM-dd HH:MM:SS


下一篇:获取两个时间之间的天数