var target=[]
var time_id=[]
function show_date_time_0(){
setTimeout("show_date_time_0()", 1000);
for (var i=0,j=target.length;i<j;i++){
today=new Date();
timeold=target[i]-today.getTime(); //取得指定时间与当前时间的时间差距
sectimeold=timeold/1000; //得到当前时间总数,单位以秒计算
secondsold=Math.floor(sectimeold); //以整数形式取时间总数
msPerDay=24*60*60*1000; //以秒为单位,计算一天的时间
e_daysold=timeold/msPerDay; //取得差距的时间以天为单位
daysold=Math.floor(e_daysold); //以天为单位取整时间
e_hrsold=(e_daysold-daysold)*24; //取小时
hrsold=Math.floor(e_hrsold); //小时取整时
e_minsold=(e_hrsold-hrsold)*60; //取分钟
minsold=Math.floor((e_hrsold-hrsold)*60); //取整分钟
seconds=Math.floor((e_minsold-minsold)*60); //取整秒数
if (daysold<0) {
document.getElementById(time_id[i]).innerHTML="团购已经结束啦!";
}
else {
if (minsold<10) {minsold="0"+minsold}
if (seconds<10) {seconds="0"+seconds}
if (daysold<3) {
document.getElementById(time_id[i]).innerHTML="<font color=red>"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒</font>";
}
else {
document.getElementById(time_id[i]).innerHTML=daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
}
}
}
}
setTimeout("show_date_time_0()", 1000);
<div id="TimeCounter_1" class="time_gg"></div>
<SCRIPT>
function day(){
var d=new Date(2010,9,21);//这个地方我想要(yy-MM-dd hh:mm:ss)这种格式的因为要读取数据库里的时间格式就是这个样子的
var year=d.getYear();
var mon=d.getMonth()-1;
var day=d.getDate();
target[target.length]=new Date(year,mon,day).getTime();
time_id[time_id.length]="TimeCounter_1"
}
day();
</SCRIPT>
var time_id=[]
function show_date_time_0(){
setTimeout("show_date_time_0()", 1000);
for (var i=0,j=target.length;i<j;i++){
today=new Date();
timeold=target[i]-today.getTime(); //取得指定时间与当前时间的时间差距
sectimeold=timeold/1000; //得到当前时间总数,单位以秒计算
secondsold=Math.floor(sectimeold); //以整数形式取时间总数
msPerDay=24*60*60*1000; //以秒为单位,计算一天的时间
e_daysold=timeold/msPerDay; //取得差距的时间以天为单位
daysold=Math.floor(e_daysold); //以天为单位取整时间
e_hrsold=(e_daysold-daysold)*24; //取小时
hrsold=Math.floor(e_hrsold); //小时取整时
e_minsold=(e_hrsold-hrsold)*60; //取分钟
minsold=Math.floor((e_hrsold-hrsold)*60); //取整分钟
seconds=Math.floor((e_minsold-minsold)*60); //取整秒数
if (daysold<0) {
document.getElementById(time_id[i]).innerHTML="团购已经结束啦!";
}
else {
if (minsold<10) {minsold="0"+minsold}
if (seconds<10) {seconds="0"+seconds}
if (daysold<3) {
document.getElementById(time_id[i]).innerHTML="<font color=red>"+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒</font>";
}
else {
document.getElementById(time_id[i]).innerHTML=daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
}
}
}
}
setTimeout("show_date_time_0()", 1000);
<div id="TimeCounter_1" class="time_gg"></div>
<SCRIPT>
function day(){
var d=new Date(2010,9,21);//这个地方我想要(yy-MM-dd hh:mm:ss)这种格式的因为要读取数据库里的时间格式就是这个样子的
var year=d.getYear();
var mon=d.getMonth()-1;
var day=d.getDate();
target[target.length]=new Date(year,mon,day).getTime();
time_id[time_id.length]="TimeCounter_1"
}
day();
</SCRIPT>
var refreshTime = 1000 ;
var countDownItems = {};
var identity = 1 ;
function getIdentity(){
return identity++ ;
}
var tempCountDown={
refreshTime:refreshTime,
create:function(endTime){
var itemKey = new Date().getTime() +""+getIdentity();
countDownItems[itemKey]={endTime:endTime};
this.exec(this.Event.ON_CREATE,[itemKey]);
return itemKey ;
},
remove:function(itemKey){
delete countDownItems[itemKey] ;
this.exec(this.Event.ON_REMOVE,[itemKey]);
},
Event:{
ON_REFRESH:"refresh",
ON_CREATE:"create",
ON_REMOVE:"remove"
}
}
Listener.apply(tempCountDown);
window.setTimeout(function(){
for(var i in countDownItems){
var spaceTime= countDownItems[i].endTime.getTime()-new Date().getTime();
var nD =Math.floor(spaceTime/(1000 * 60 * 60 * 24));
var nH=Math.floor(spaceTime/(1000*60*60)) % 24;
var nM=Math.floor(spaceTime/(1000*60)) % 60;
var nS=Math.floor(spaceTime/1000) % 60;
countDownItems[i]["countDown"] = {
day:nD,
hour:nH,
minute:nM,
second:nS,
toString:function(){
return this.day+"天"+this.hour+"小时"+ this.minute+"分"+this.second+"秒" ;
}
}
tempCountDown.exec(DateCountDown.Event.ON_REFRESH,[i,countDownItems[i]["countDown"]])
}
window.setTimeout(arguments.callee,DateCountDown.refreshTime);
},refreshTime) ;
return tempCountDown ;
}).call(null);下面是应用实例<script type="text/javascript" src="Listener.js"></script>
<script type="text/javascript" src="DateCountDown.js"></script>
<script>
DateCountDown.addListener(DateCountDown.Event.ON_CREATE,function(i){
var div = document.createElement("div") ;
document.body.appendChild(div);
div.id=i;
});
DateCountDown.addListener(DateCountDown.Event.ON_REFRESH,function(i,cd){
document.getElementById(i).innerHTML = cd ;
});
window.onload = function(){
var d1= new Date();
d1.setHours(17);
d1.setMinutes(30);
d1.setSeconds(00);
DateCountDown.create(d1);
}
</script>