分享一例javascript代码,实现DateAdd()方法,我们知道javascript中是没有日期相加的方法的,正好本文给出的例子可以实现,有用到的朋友参考下吧。
javascript dateadd()方法的实现代码,如下:
复制代码 代码示例:
function DateAdd (interval,number,date){
//确保为date类型:
date=convertToDate(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));
}
};
function dateFormat(date){
//确保为date类型:
date=convertToDate(date);
var defyear = parseInt(date.getFullYear());//當前年
var defmonth = parseInt(date.getMonth()+1,10); //當前月
var defday=date.getDate();//當前日
var result="";
if(defmonth<10&&defday<10){
result=defyear+'-0'+defmonth+'-0'+defday;
}else if(defmonth<10){
result=defyear+'-0'+defmonth+'-'+defday;
}else if(defday<10){
result=defyear+'-'+defmonth+'-0'+defday;
}else{ www.jb200.com
result=defyear+'-'+defmonth+'-'+defday;
}
return result;
};
//javascript中定義的replaceAll()
String.prototype.replaceAll = function(s1,s2){
return this.replace(new RegExp(s1,"gm"),s2);
};
//將日期類型格式的字符串轉化為日期類型:
function convertToDate(expr){
if(typeof expr=='string'){
expr=expr.replaceAll('-','/');//將字符中的-替換為/,原因是IE或其它瀏覽器不支持-符號的Date.parse()
return new Date(Date.parse(expr));
}else{
return expr;
}
};