显示一个距离2012年凌晨零点的倒计时秒表
html代码:
<input id="timeTxt" size="80"><button onclick='int = setInterval(countdown,1000)' />开始计时</button><button onclick='clearInterval(int)'>停止计时</button>
js代码:
function countdown(){
var n = new Date();
var ed = new Date(2011,11,31,23,59,59) //将2011年12月31号23时59分59秒存入对象data2中,显示为Thu Sep 8 13:40:59 UTC+0800 2011,如是2012年凌晨零点,也可以直接写2012,0,1,0,0,0,那后面写出秒的时候就直接用s,不需要s+1了
//都是以毫秒为单位,所以要除以1000,得到的就是二者相距的秒数
t = (ed-n)/1000;
//取时间要向下舍入,比如3.5天,肯定是算3天12小时,所以要用floor()
var d = Math.floor(t/(24*60*60));
var h = Math.floor(t%(24*60*60)/(60*60));
var m = Math.floor(t%(60*60)/60);
var s = Math.floor(t%60);
document.getElementById("timeTxt").value = "距离2012年还剩"+d+"天"+h+"小时"+m+"分钟"+(s+1)+"秒"
}