zl程序教程

您现在的位置是:首页 >  前端

当前栏目

JavaScript版DateAdd和DateDiff函数代码

JavaScript代码 函数 datediff DATEADD
2023-06-13 09:14:33 时间

DateAdd函数:

复制代码代码如下:

functionDateAdd(interval,number,date){
switch(interval.toLowerCase()){
case"y":returnnewDate(date.setFullYear(date.getFullYear()+number));
case"m":returnnewDate(date.setMonth(date.getMonth()+number));
case"d":returnnewDate(date.setDate(date.getDate()+number));
case"w":returnnewDate(date.setDate(date.getDate()+7*number));
case"h":returnnewDate(date.setHours(date.getHours()+number));
case"n":returnnewDate(date.setMinutes(date.getMinutes()+number));
case"s":returnnewDate(date.setSeconds(date.getSeconds()+number));
case"l":returnnewDate(date.setMilliseconds(date.getMilliseconds()+number));
}
}

DateDiff函数:
复制代码代码如下:

functionDateDiff(interval,date1,date2){
varlong=date2.getTime()-date1.getTime();//相差毫秒
switch(interval.toLowerCase()){
case"y":returnparseInt(date2.getFullYear()-date1.getFullYear());
case"m":returnparseInt((date2.getFullYear()-date1.getFullYear())*12+(date2.getMonth()-date1.getMonth()));
case"d":returnparseInt(long/1000/60/60/24);
case"w":returnparseInt(long/1000/60/60/24/7);
case"h":returnparseInt(long/1000/60/60);
case"n":returnparseInt(long/1000/60);
case"s":returnparseInt(long/1000);
case"l":returnparseInt(long);
}
}

兼容多浏览器的datediff函数
复制代码代码如下:
<scripttype="text/javascript">
functionNewDate(str){
str=str.split("-");
vardate=newDate();
date.setUTCFullYear(str[0],str[1]-1,str[2]);
date.setUTCHours(0,0,0,0);
returndate;
}
functionTimeCom(dateValue){
varnewCom;

if(dateValue==""){
newCom=newDate();
}else{
newCom=NewDate(dateValue);
}
this.year=newCom.getYear();
this.month=newCom.getMonth()+1;
this.day=newCom.getDate();
this.hour=newCom.getHours();
this.minute=newCom.getMinutes();
this.second=newCom.getSeconds();
this.msecond=newCom.getMilliseconds();
this.week=newCom.getDay();
}
functionDateDiff(interval,date1,date2){
varTimeCom1=newTimeCom(date1);
varTimeCom2=newTimeCom(date2);
varresult;
switch(String(interval).toLowerCase()){
case"y":
case"year":
result=TimeCom1.year-TimeCom2.year;
break;
case"m":
case"month":
result=(TimeCom1.year-TimeCom2.year)*12+(TimeCom1.month-TimeCom2.month);
break;
case"d":
case"day":
result=Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day))/(1000*60*60*24));
break;
case"h":
case"hour":
result=Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour))/(1000*60*60));
break;
case"min":
case"minute":
result=Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute))/(1000*60));
break;
case"s":
case"second":
result=Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute,TimeCom1.second)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute,TimeCom2.second))/1000);
break;
case"ms":
case"msecond":
result=Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute,TimeCom1.second,TimeCom1.msecond)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute,TimeCom2.second,TimeCom1.msecond);
break;
case"w":
case"week":
result=Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day))/(1000*60*60*24))%7;
break;
default:
result="invalid";
}
return(result);
}
</script>