首先上一段代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>脚本学堂倒计时js代码 - http://www.jb200.com/</title>
</head>
<body>
<DIV id="CountMsg" class="HotDate">
<span id="t_d">00天</span>
<span id="t_h">00时</span>
<span id="t_m">00分</span>
<span id="t_s">00秒</span>
</DIV>
<script type="text/javascript">
function getRTime(){
var EndTime= new Date('2013/05/1 10:00:00'); //截止时间 前端路上
var NowTime = new Date();
var t =EndTime.getTime() - NowTime.getTime();
/*var d=Math.floor(t/1000/60/60/24);
t-=d*(1000*60*60*24);
var h=Math.floor(t/1000/60/60);
t-=h*60*60*1000;
var m=Math.floor(t/1000/60);
t-=m*60*1000;
var s=Math.floor(t/1000);*/
var d=Math.floor(t/1000/60/60/24);
var h=Math.floor(t/1000/60/60%24);
var m=Math.floor(t/1000/60%60);
var s=Math.floor(t/1000%60);
document.getElementById("t_d").innerHTML = d + "天";
document.getElementById("t_h").innerHTML = h + "时";
document.getElementById("t_m").innerHTML = m + "分";
document.getElementById("t_s").innerHTML = s + "秒";
}
setInterval(getRTime,1000);
</script>
</body>
</html>
这里是一个JS中显示倒计时的代码,从10一秒一秒地往下减直到计时结束,代码如下
runCount(10);
function runCount(t){
if(t>0){
document.getElementById(‘shownum').innerHTML = t;
t–;
setTimeout(function(){runCount(t);},1000);
}else{
document.getElementById(‘shownum').innerHTML = ‘倒计时结束!';
}
}
运行该js代码前,需要先添加一个id为shownum的div。
js实现动态倒计时功能 * a是从后台接收到的时间戳,需转换成毫秒单位
<div>
距明年还有:
<span id='daya'></span>天
<span id='hoursa'></span>小时
<span id='minua'></span>分
<span id='secoa'></span>秒
</div>
<script type="text/javascript">
/**
*
* copyright WE 2012.7
* js实现动态倒计时功能
* a是从后台接收到的时间戳,需转换成毫秒单位
*
*/
var a=12345678; //以毫秒为单位
function fomtime()
{
a=a-1000;
var b=new Date();
b.setTime(0);
var c=new Date();
c.setTime(a);
var day1=b.getDate(); //为方便调用,把天数、小时等单独定义www.jb200.com
var hours1=b.getHours();
var minu1=b.getMinutes();
var seco1=b.getSeconds();
var day2=c.getDate();
var hours2=c.getHours();
var minu2=c.getMinutes();
var seco2=c.getSeconds();
var day=day2-day1;
var hours=hours2-hours1;
var minu=minu2-minu1;
var seco=seco2-seco1;
document.getElementById('daya').innerHTML=day;
document.getElementById('hoursa').innerHTML=hours;
document.getElementById('minua').innerHTML=minu;
document.getElementById('secoa').innerHTML=seco;
setTimeout("fomtime()",1000);
}
fomtime();
</script>
按天倒计时
代码如下:
<Script Language="JavaScript">
<!-- Begin
var timedate= new Date("January 14,2028");
var times="www.jb200.com发工资";
var now = new Date();
var date = timedate.getTime() - now.getTime();
var time = Math.floor(date / (1000 * 60 * 60 * 24));
if (time >= 0) ;
document.write("<li><font color=#DEDBDE>现在离2028年"+times+"还有: <font color=#ffffff><b>"+time +"</b></font> 天</font></li>");
// End -->
</Script>