时间:2021-05-26
DateAdd函数:
复制代码 代码如下:
function DateAdd(interval,number,date){
switch(interval.toLowerCase()){
case "y": return new Date(date.setFullYear(date.getFullYear()+number));
case "m": return new Date(date.setMonth(date.getMonth()+number));
case "d": return new Date(date.setDate(date.getDate()+number));
case "w": return new Date(date.setDate(date.getDate()+7*number));
case "h": return new Date(date.setHours(date.getHours()+number));
case "n": return new Date(date.setMinutes(date.getMinutes()+number));
case "s": return new Date(date.setSeconds(date.getSeconds()+number));
case "l": return new Date(date.setMilliseconds(date.getMilliseconds()+number));
}
}
DateDiff函数:
复制代码 代码如下:
function DateDiff(interval,date1,date2){
var long = date2.getTime() - date1.getTime(); //相差毫秒
switch(interval.toLowerCase()){
case "y": return parseInt(date2.getFullYear() - date1.getFullYear());
case "m": return parseInt((date2.getFullYear() - date1.getFullYear())*12 + (date2.getMonth()-date1.getMonth()));
case "d": return parseInt(long/1000/60/60/24);
case "w": return parseInt(long/1000/60/60/24/7);
case "h": return parseInt(long/1000/60/60);
case "n": return parseInt(long/1000/60);
case "s": return parseInt(long/1000);
case "l": return parseInt(long);
}
}
兼容多浏览器的datediff函数
复制代码 代码如下:
<script type="text/javascript">
function NewDate(str) {
str = str.split('-');
var date = new Date();
date.setUTCFullYear(str[0], str[1] - 1, str[2]);
date.setUTCHours(0, 0, 0, 0);
return date;
}
function TimeCom(dateValue) {
var newCom;
if (dateValue == "") {
newCom = new Date();
} 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();
}
function DateDiff(interval, date1, date2) {
var TimeCom1 = new TimeCom(date1);
var TimeCom2 = new TimeCom(date2);
var result;
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>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了SQLServer日期加减函数DATEDIFF与DATEADD用法。分享给大家供大家参考,具体如下:SQLServer日期的加减函数:DATEDI
ASP中DateDiff函数详解DateDiff函数描述返回两个日期之间的时间间隔。语法DateDiff(interval,date1,date2[,first
扩展php中的时间函数DateDiff和DateAddfunctionDateDiff($part,$begin,$end){$diff=strtotime($
DateDiff函数返回两个日期之间的时间间隔。DateDiff(interval,date1,date2[,firstdayofweek[,firstweek
ASP里面计算两个时间的差距可以DateDiff,但javascript里面没有,需要手写函数。js里面居然可以直接Date1-Date2,而且还是转换为毫秒计