var tempdate = new Date();
//下个月开始日期
var newdate = new Date(tempdate.getFullYear,tempdate.getMonth+1,1);
alert(newdate .toLocaleString());
//下个月相对日期
newdate = new Date(tempdate.getFullYear,tempdate.getMonth+1,tempdate.getDate);
alert(newdate .toLocaleString());
//明年相对日期
newdate = new Date(tempdate.getFullYear+1,tempdate.getMonth+1,tempdate.getDate);
alert(newdate .toLocaleString());
//下个月开始日期
var newdate = new Date(tempdate.getFullYear,tempdate.getMonth+1,1);
alert(newdate .toLocaleString());
//下个月相对日期
newdate = new Date(tempdate.getFullYear,tempdate.getMonth+1,tempdate.getDate);
alert(newdate .toLocaleString());
//明年相对日期
newdate = new Date(tempdate.getFullYear+1,tempdate.getMonth+1,tempdate.getDate);
alert(newdate .toLocaleString());
//下个月开始日期
var newdate = new Date(tempdate.getFullYear(),tempdate.getMonth()+1,1);
alert(newdate .toLocaleString());
//下个月相对日期
newdate = new Date(tempdate.getFullYear(),tempdate.getMonth()+1,tempdate.getDate());
alert(newdate .toLocaleString());
//明年相对日期
newdate = new Date(tempdate.getFullYear()+1,tempdate.getMonth()+1,tempdate.getDate());
alert(newdate .toLocaleString());
Date.prototype.addMonth = function(num){this.setMonth(this.getMonth() + parseNum(num));return this;}
Date.prototype.addDay = function(num){this.setDate(this.getDate() + parseNum(num));return this;}
Date.prototype.addHour = function(num){this.setHours(this.getHours() + parseNum(num));return this;}
Date.prototype.addMinute = function(num){this.setMinutes(this.getMinutes() + parseNum(num));return this;}
Date.prototype.addSecond = function(num){this.setSeconds(this.getSeconds() + parseNum(num));return this;}
Date.prototype.addMS = function(num){this.setMilliseconds(this.getMilliseconds() + parseNum(num));return this;}
Date.prototype.addWeek = function(num){this.setDate(this.getDate() + parseNum(num) * 7);return this;}
function parseNum(num)
{
if(num!=null)
{
switch(typeof(num))
{
case "number":return num;
case "string":var re = /\d+/;return re.test(num)?parseInt(num.match(re)):0;
case "boolean":return num?1:0;
case "object":
if(num.value!=null)return parseNum(num.value);
if(num.innerText!=null)return parseNum(num.innerText);
return 0;
default :
return 0;
}
}
else
return 0;
}
//加n年
aa.addYear(1);//aa.addYear(-1); aa.addYear("1");aa.addYear("-100");aa.addYear(true);
alert(aa.toLocaleString());
//其它类推
//addMonth 月
//addDay 天
//addHour 小时
//addMinute 分钟
//addSecond 秒
//addMS 千分秒
//addWeek 星期
注:其中一段是梅花雪的//default format
Date.prototype.format="yyyy-MM-dd hh:mm:ss";
//used for cache the RegularExpressions
Date.prototype.Regex={
"regex1" : /([\$\(\)\*\+\.\[\]\?\\\^\{\}\|])/g ,
"regex2" : /(y+|M+|d+|m+|h+|s+)/g ,
"year1" : /^y{1,2}$/ ,
"year2" : /^y{4}$/ ,
"month" : /^M{1,2}$/ ,
"day" : /^d{1,2}$/ ,
"hour" : /^h{1,2}$/i ,
"minute" : /^m{1,2}$/ ,
"second" : /^s{1,2}$/
}
//var aa = new Date();
//aa.setFormatDate("2005-12-30","yyyy-MM-dd");
//aa.format = "yyyy-MM-dd";
//aa.setFormatDate("2006-11-11")
//[extended method] Date.setDate by format string
//Warning : date string must perfect than format string
Date.prototype.setFormatDate = function() //author : gzdiablo
{
var A = arguments;
var inputStr = "";
var sformat = this.format;
if(A.length==0 && A.length>2){return this;}
if(A.length==1){inputStr = A[0];}
if(A.length==2){inputStr = A[0];sformat = A[1];}
if(sformat == null && inputStr == null)return this;
var tempint = Date.parse(inputStr);
if(!isNaN(tempint))this.setTime(tempint);
var tempregex = new RegExp(sformat.replace(this.Regex["regex1"],"\\$1").replace(this.Regex["regex2"],"(\\d+)"));
var temparr = sformat.match(this.Regex["regex2"]);
var temparr2 = inputStr.match(tempregex);
if(temparr2!=null && temparr!=null)
{
if(temparr2.length>1)temparr2.shift();
for(var i=0;i<temparr.length && temparr2[i]!=null;i++)
{
switch(true)
{
case this.Regex["year1"].test(temparr[i]):this.setFullYear(temparr2[i]*1 + 2000);break;
case this.Regex["year2"].test(temparr[i]):this.setFullYear(temparr2[i]*1);break;
case this.Regex["month"].test(temparr[i]):this.setMonth(temparr2[i]*1-1);break;
case this.Regex["day"].test(temparr[i]):this.setDate(temparr2[i]*1);break;
case this.Regex["hour"].test(temparr[i]):this.setHours(temparr2[i]*1);break;
case this.Regex["minute"].test(temparr[i]):this.setMinutes(temparr2[i]*1);break;
case this.Regex["second"].test(temparr[i]):this.setSeconds(temparr2[i]*1);break;
}
}
}
return this;
}
//[extended method] Date.toString by format string
Date.prototype.Format = function(informat) //author: meizz
{
var inputStr = "";
var format = this.format;
if(informat!=null)format = informat;
var o = {
"M+" : this.getMonth()+1,
"d+" : this.getDate(),
"h+" : this.getHours(),
"H+" : this.getHours(),
"m+" : this.getMinutes(),
"s+" : this.getSeconds()
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
{
format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k]:("00"+ o[k]).substr((""+ o[k]).length));
}
return format;
}Date.prototype.addYear = function(num){this.setFullYear(this.getFullYear() + parseNum(num));return this;}
Date.prototype.addMonth = function(num){this.setMonth(this.getMonth() + parseNum(num));return this;}
Date.prototype.addDay = function(num){this.setDate(this.getDate() + parseNum(num));return this;}
Date.prototype.addHour = function(num){this.setHours(this.getHours() + parseNum(num));return this;}
Date.prototype.addMinute = function(num){this.setMinutes(this.getMinutes() + parseNum(num));return this;}
Date.prototype.addSecond = function(num){this.setSeconds(this.getSeconds() + parseNum(num));return this;}
Date.prototype.addMS = function(num){this.setMilliseconds(this.getMilliseconds() + parseNum(num));return this;}
Date.prototype.addWeek = function(num){this.setDate(this.getDate() + parseNum(num) * 7);return this;}
如果你确保输入的一定是数字的话 把上面的 parseNum(num); 改成num; 就好当然也不用这个parseNum的方法了