倒计时原理
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>倒计时原理</title>
<style>
</style>
<script>
window.onload=function(){
//现在的时间点(在变)
//未来的时间点(不变)
var oBody=document.body;
setInterval(autoTime,1000);
autoTime();
function autoTime(){
var iNow=new Date();
var iNew=new Date(2017,2,22,14,50,0);
var t=Math.floor((iNew-iNow)/1000);//毫秒转换为秒
var str=Math.floor(t/86400)+'天'+toTwo(Math.floor(t%86400/3600))+'时'+toTwo(Math.floor(t%86400%3600/60))+'分'+toTwo(t%60)+'秒';
oBody.innerHTML=str;
}
//天:Math.floor(t/86400)
//时:Math.floor(t%86400/3600)
//分:Math.floor(t%86400%3600/60)
//秒:t%60;
//数字形式:new Date(2017,2,21,14,50,0);
//字符串形式:new Date('June 10,2013,12:12:12');
}
function toTwo(n){
return n<10?'0'+n:''+n;
}
</script>
</head>
<body>
</body>
</html>