JS当前日期相加相减

时间:2023-12-18 22:26:44
function DateAddORSub(interval,type,number)
{
/*
 * 功能:实现Script的Date加减功能.
 * 参数:interval,字符串表达式,表示要添加的时间间隔.
 * 参数:number,数值表达式,表示要添加的时间间隔的个数.
 * 参数:type,加减类型.
 * 返回:新的时间对象.
 * var newDate =DateAddORSub("d","+",5);
 */
  var date = new Date();
    switch(interval)
    {
        case "y" : {
            if(type=="+"){
             date.setFullYear(date.getFullYear()+number);
            }else{
             date.setFullYear(date.getFullYear()-number);
            }
            return date;
            break;
        }
        case "q" : {
            if(type=="+"){
            date.setMonth(date.getMonth()+number*3);
            }else{
             date.setMonth(date.getMonth()-number*3);
            }
            return date;
            break;
        }
        case "m" : {
            if(type=="+"){
             date.setMonth(date.getMonth()+number);
            }else{
             date.setMonth(date.getMonth()-number);
            }
            return date;
            break;
        }
        case "w" : {
            if(type=="+"){
            date.setDate(date.getDate()+number*7);
            }else{
            date.setDate(date.getDate()-number*7);
            }
            return date;
            break;
        }
        case "d" : {
            if(type=="+"){
             date.setDate(date.getDate()+number);
            }else{
              date.setDate(date.getDate()-number);
            }
            return date;
            break;
        }
        case "h" : {
             if(type=="+"){
            date.setHours(date.getHours()+number);
            }else{
             date.setHours(date.getHours()-number);
            }
            return date;
            break;
        }
        case "m" : {
            if(type=="+"){
            date.setMinutes(date.getMinutes()+number);
            }else{
            date.setMinutes(date.getMinutes()-number);
            }
            return date;
            break;
        }
        case "s" : {
             if(type=="+"){
            date.setSeconds(date.getSeconds()+number);
            }else{
             date.setSeconds(date.getSeconds()-number);
            }
            return date;
            break;
        }
        default : {
             if(type=="+"){
             date.setDate(d.getDate()+number);
            }else{
             date.setDate(d.getDate()-number);
            }
            return date;
            break;
        }
    }
}

function formatDate(date){;
   var year = date.getFullYear();       //年
   var month = date.getMonth() + 1;     //月
   var day = date.getDate();            //日
   return year+"-"+month+"-"+day;
 }
  
//当前时间加五天.  
var newDate = DateAddORSub("d","+",5);  
alert(newDate.toLocaleDateString());  
//当前时间减五天.  
newDate = DateAddORSub("d","-",5);  
alert(formatDate(newDate));  
//当前时间加两个月.  
newDate = DateAddORSub("m","+",2);  
alert(newDate.toLocaleDateString());  
//当前时间减两个月.  
newDate = DateAddORSub("m","-",2);  
alert(formatDate(newDate));  
//当前时间加一年  
newDate = DateAddORSub("y","+",1);  
alert(newDate.toLocaleDateString());  
//当前时间减一年  
newDate = DateAddORSub("y","-",1);  
alert(formatDate(newDate));