javascript 给指定日期加上多少天 月 年

function   DateAdd(interval,number,date) 

/*
  *   功能:实现javascript的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   "q "   :   {  //加季度
                        date.setMonth(date.getMonth()+number*3); 
                        return   date; 
                        break; 
                } 
                case   "m "   :   {  //加月
                        date.setMonth(date.getMonth()+number); 
                        return   date; 
                        break; 
                } 
                case   "w "   :   {   //加星期
                        date.setDate(date.getDate()+number*7); 
                        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; 
                } 
        } 

 
var   now   =   new   Date(); 
//加五天. 
var   newDate   =   DateAdd( "d ",5,now); 
alert(newDate.toLocaleDateString()) 
//加两个月. 
newDate   =   DateAdd( "m ",2,now); 
alert(newDate.toLocaleDateString()) 
//加一年 
newDate   =   DateAdd( "y ",1,now); 
alert(newDate.toLocaleDateString()) 

 

 

/*

自己写的加月的

*/

function   DateAdd(interval,number,date){
        switch(interval)
        {
                case   "y "   :   {
                        date.setFullYear(date.getFullYear()+number);
                        return   date;
                        break;
                }
                case   "q "   :   {
                        date.setMonth(date.getMonth()+number*3);
                        return   date;
                        break;
                }
                case   "m "   :   {
                        date.setMonth(date.getMonth()+number);
                        return   date;
                        break;
                }
                case   "w "   :   {
                        date.setDate(date.getDate()+number*7);
                        return   date;
                        break;
                }
                case   "d "   :   {
                        date.setDate(date.getDate()+number);
                        return   date;
                        break;
                }
               
              
                default   :   {
                        date.setDate(d.getDate()+number);
                        return   date;
                        break;
                }
        }
    }

 

//有可能会用到

//将字符串转化为日期型
    function getStrToDate(strDate) {
        var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
         function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
        return date;
    }

//格式化日期
    function getNowFormatDate(crDate){
    var Year = 0;
    var Month = 0;
    var Day = 0;
    var CurrentDate = "";
    Year= crDate.getFullYear();//ie火狐下都可以
    Month= crDate.getMonth()+1;
    Day = crDate.getDate();
    CurrentDate += Year + "-";
    if (Month >= 10 )
    {
    CurrentDate += Month + "-";
    }
    else
    {
    CurrentDate += "0" + Month + "-";
    }
    if (Day >= 10 )
    {
    CurrentDate += Day ;
    }
    else
    {
    CurrentDate += "0" + Day ;
    }
    return CurrentDate;
    }

你可能感兴趣的:(JavaScript)