js 时间差(过去了多少时间、还剩多少时间)

发布时间:2018-10-13 13:54:15 作者:Aoe Wide 阅读量:3021

<script type="text/javascript">
//设置多少分钟后结束
var KSSJ=Number("120");
//结束考试时间
var newDate=DateAdd( "m",KSSJ,CJKSSJ);
//开始考试时间
var timestamp = Date.parse(new Date());
 
setInterval("setTimer(timestamp)",1000); 
 
function setTimer(timestamp){
	$("#yiyong").html("已用:"+timeago(timestamp));
	$("#shengyu").html("剩余:"+timediff(newDate));
}
 
//计算已用多少时间
function timeago(timestamp){
	   var leftTime = (new Date())- (timestamp); //计算已用的毫秒数   
	   var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数   
	   var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时   
	   var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟   
	   var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数   
	   days = checkTime(days);   
	   hours = checkTime(hours);   
	   minutes = checkTime(minutes);   
	   seconds = checkTime(seconds);   
		var timehtml= hours+":" + minutes+":"+seconds; 
		return timehtml;
	} 
//计算剩余多少时间
function timediff(times){
		var leftTime = (new Date(times)) - (new Date()); 	  
		var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数   
	   var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时   
	   var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟   
	   var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数   
	   days = checkTime(days);   
	   hours = checkTime(hours);   
	   minutes = checkTime(minutes);   
	   seconds = checkTime(seconds);   
		var timehtml= hours+":" + minutes+":"+seconds; 
		return timehtml;
	} 
function checkTime(i){ 
	   //将0-9的数字前面加上0,例1变为01   
	   if(i<10)   { 
	   i = "0" + i;  
	   }   
	   return i; 
}
function   DateAdd(interval,number,date)  
{  
/* 
  *   功能:实现VBScript的DateAdd功能. 
  *   参数:interval,字符串表达式,表示要添加的时间间隔. 
  *   参数:number,数值表达式,表示要添加的时间间隔的个数. 
  *   参数:date,时间对象. 
  *   返回:新的时间对象. 
  *   var   now   =   new   Date(); 
  *   var   newDate   =   DateAdd( "d ",5,now); 
  *---------------   DateAdd(interval,number,date)   ----------------- 
  */  
        switch(interval)  
        {  
                case   "y":   {  
                        date.setFullYear(date.getFullYear()+number);  
                        return   date;  
                        break;  
                }   
                case   "M":   {  
                        date.setMonth(date.getMonth()+number);  
                        return   date;  
                        break;  
                }   
                case   "d":   {  
                        date.setDate(date.getDate()+number);  
                        return   date;  
                        break;  
                }  
                case   "h":   {  
                        date.setHours(date.getHours()+number);  
                        return   date;  
                        break;  
                }  
                case   "m":   {  
                        date.setMinutes(date.getMinutes()+number);  
                        return   date;  
                        break;  
                }  
                case   "s":   {  
                        date.setSeconds(date.getSeconds()+number);  
                        return   date;  
                        break;  
                }  
                default   :   {  
                        date.setDate(d.getDate()+number);  
                        return   date;  
                        break;  
                }  
        }  
} 
</script>

我要评论

Aoe WideAoe Wide   我要留言
Aoe Wide
Catfish(鲶鱼) Blog V 2.0.30