首页 > 代码库 > 倒数日期

倒数日期

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>倒数日期</title>
<style>
div{width:500px;height:200px;}
#box1{}
div span{color:#ffcc00; }
div input{width:60px; text-align:center;}
#submit{width:40px;}
</style>
<script>
window.onload=function()
{
var aInput = document.getElementsByTagName(‘input‘);
var oTarget = document.getElementById(‘target‘);
var oRemain = document.getElementById(‘remain‘);
var oSubmit = document.getElementById(‘submit‘);
var str;
oSubmit.onclick = function(){
oTarget.innerHTML = aInput[0].value+‘年‘+aInput[1].value+‘月‘+aInput[2].value+‘日‘;

function remaintime()
{
var iNowTime = new Date();
var iEndTime = new Date();
iEndTime.setFullYear(aInput[0].value);

iEndTime.setMonth(aInput[1].value-1);
iEndTime.setDate(aInput[2].value);
iEndTime.setHours(0);
iEndTime.setMinutes(0);
iEndTime.setSeconds(0);

var iday = parseInt((iEndTime.getTime()-iNowTime.getTime())/86400000);
var ihour = parseInt((iEndTime.getTime()-iNowTime.getTime())%86400000/3600000);
var iminute = parseInt((iEndTime.getTime()-iNowTime.getTime())%3600000/60000);
var isecond = parseInt((iEndTime.getTime()-iNowTime.getTime())%60000/1000);
if(iday>=0&&iday<10)
{
iday=‘00‘+iday;
}
else if(iday>=10&&iday<100)
{
iday=‘0‘+iday;
}
if(iday<0)
{
iday=‘‘+iday;
}

ihour=ihour>9?ihour:(‘0‘+ihour);
iminute=iminute>9?iminute:(‘0‘+iminute);
isecond = isecond>9?isecond:(‘0‘+isecond);

oRemain.innerHTML = iday+‘天‘+ihour+‘时‘+iminute+‘分‘+isecond+‘秒‘;
}

remaintime();
setInterval(remaintime,1000);

}

};

</script>

</head>

<body>
<div id="box1">
<span>设置某一日期:</span>
<input type="text">年<input type="text">月<input type="text">日
<input id = ‘submit‘ type="button" value="http://www.mamicode.com/提交"><br />
<p>现在距离<strong id =‘target‘>0000年00月00日</strong>还有:</p>
<p> <strong id=‘remain‘>000天00时00分00秒</strong></p>
</body>
</html>

倒数日期