var
interval
=
1000
function
ShowCountDown(year,month,day,divname)
{
var
now
=
new
Date()
var
endDate
=
new
Date(year,
month-1,
day)
var
leftTime=endDate.getTime()-now.getTime()
var
leftsecond
=
parseInt(leftTime/1000)
//var
day1=parseInt(leftsecond/(24*60*60*6))
var
day1=Math.floor(leftsecond/(60*60*24))
var
hour=Math.floor((leftsecond-day1*24*60*60)/3600)
var
minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60)
var
second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60)
var
cc
=
document.getElementById(divname)
cc.innerHTML
=
"脚本之家提示距离"+year+"年"+month+"月"+day+"日还有:"+day1+"天"+hour+"小时"+minute+"分"+second+"秒"
}
window.setInterval(function(){ShowCountDown(2010,4,20,'divdown1')},
interval)
[Ctrl+A
全选
注:如需引入外部Js需刷新才能执行]
<SCRIPT LANGUAGE="JavaScript"><!--
var maxtime = 60*60 //一个小时,按秒计算,自己调整!
function CountDown(){
if(maxtime>=0){
minutes = Math.floor(maxtime/60)
seconds = Math.floor(maxtime%60)
minutes = minutes>=10?minutes:'0'+minutes
seconds = seconds>=10?seconds:'0'+seconds
msg = "距离结束还有"+minutes+"分"+seconds+"秒"
document.all["timer"].innerHTML=msg
if(maxtime == 5*60) alert('注意,还有5分钟!')
--maxtime
}
else{
clearInterval(timer)
alert("时间到,结束!")
}
}
timer = setInterval("CountDown()",1000)
//-->
</SCRIPT>
<meta charset="utf-8">
<div id="timer" style="color:red"></div>
逻辑如下:
1、先判断两个时间的时间差,当然如果得到的时间是13位时间戳的话你需要转化一下
2、判断时间够一天显示天数,够一小时显示小时数
相关代码:
//获取时间,时间戳转换,不是则不转var mydate1 = new Date(parseInt(time1.replace("/Date(", "").replace(")/", "")))
var mydate2 = new Date(parseInt(time2.replace("/Date(", "").replace(")/", "")))
//获取剩余秒数
var timerc =dateDiff(mydate, '2015/05/19 10:00:00') / 1000
function dateDiff(date1, date2) {
var dt1 = new Date(Date.parse(date1))
var dt2 = new Date(Date.parse(date2))
try {
return Math.round((dt2.getTime() - dt1.getTime()))
}
catch (e) {
return e.message
}
}
//循环调用倒计时
function add() { //加时函数
--timerc //时间变量自减1
var day = parseInt(timerc / 86400)
var hour = parseInt((timerc % 86400) / 3600)
var min = parseInt((timerc % 3600) / 60)
var sec = Number(parseInt(timerc % 60 / 10)).toString() + Number(parseInt((timerc % 10))).toString()
if (day > 0) { //如果不到5分钟
$("#day").html(day + '天') //写入天数
$("#hour").html(hour + '时') //写入小时数
$("#min").html(min + '分') //写入分钟数
$("#sec").html(sec + '秒') //写入秒数(两位)
}
else if (hour > 0) {
$("#hour").html(hour + '时') //写入小时数
$("#min").html(min + '分') //写入分钟数
$("#sec").html(sec + '秒') //写入秒数(两位)
}
else if (min > 0) {
$("#min").html(min + '分') //写入分钟数
$("#sec").html(sec + '秒') //写入秒数(两位)
}
else if (sec != '00') {
$("#sec").html(sec + '秒') //写入秒数(两位)
}
else {
tiao(activity) return true//时间到0调用函数
}
setTimeout("add()", 1000) //设置1000毫秒以后执行一次本函数
}
望采纳!