我的处理比较简单:如果今天是2000年2月29日,那么一年后的今天算3月1号还有就是想到闰年,大小月的问题,把我的代码晒一下吧,供有需要的人参考,也请大家指正完善,谢谢:function set_ftime(){ //根据不同的类型,设置不同的失效时间
if(form1.D1.value==2){
form1.ftime.value= addYear(1);
}else if(form1.D1.value==3){
form1.ftime.value= addYear(1);
}
}function isLeapYear(year){ //判断传入的年份这个参数是不是闰年
if((year%4==0&&year%100!=0)||year%400==0){
return true;
}else
return false;
}function addYear(years){ //得到与系统时间相差指定年份的时间 传入的参数years就是指定的年份
date=new Date();//系统日期
intYear=date.getYear();
intMonth=date.getMonth()+1;
intDay=date.getDate();
newYear=intYear+years;
var newMonth=0;
var newDate=0;
if(!isLeapYear(newYear)&&intMonth==2&&intDay==29){
newMonth=3;
newDate=1;
}else{
newMonth=intMonth;
newDate=intDay;
}
if(newMonth<10)
newMonth="0"+newMonth;
if(newDate<10)
newDate="0"+newDate;
result=newYear+"-"+newMonth+"-"+newDate;
return result;
}function addMonth(months){ //得到与系统时间相差指定月份的时间 传入的参数months就是指定的月份
date=new Date();//系统日期
intYear=date.getYear();
intMonth=date.getMonth()+1;
intDay=date.getDate();
newYear=intYear;
newMonth=intMonth+months;
var newDate=0;
if((newMonth==4||newMonth==6||newMonth==9||newMonth==11)&&intDay==31){
newMonth=newMonth+1;
newDate=1;
}else if(!isLeapYear(intYear)&&newMonth==2&&(intDay==29||intDay==30||intDay==31)){
newMonth=3;
newDate=1;
}else if(isLeapYear(intYear)&&newMonth==2&&(intDay==30||intDay==31)){
newMonth=3;
newDate=1;
}else{
newDate=intDay;
}
if(newMonth/12>0){ //如果月份加了之后超过12,则年份上也要相应增加
newYear=intYear+parseInt(newMonth/12);
newMonth=newMonth%12 ;
}
if(newMonth<10)
newMonth="0"+newMonth;
if(newDate<10)
newDate="0"+newDate;
result=newYear+"-"+newMonth+"-"+newDate;
return result;
}
if(form1.D1.value==2){
form1.ftime.value= addYear(1);
}else if(form1.D1.value==3){
form1.ftime.value= addYear(1);
}
}function isLeapYear(year){ //判断传入的年份这个参数是不是闰年
if((year%4==0&&year%100!=0)||year%400==0){
return true;
}else
return false;
}function addYear(years){ //得到与系统时间相差指定年份的时间 传入的参数years就是指定的年份
date=new Date();//系统日期
intYear=date.getYear();
intMonth=date.getMonth()+1;
intDay=date.getDate();
newYear=intYear+years;
var newMonth=0;
var newDate=0;
if(!isLeapYear(newYear)&&intMonth==2&&intDay==29){
newMonth=3;
newDate=1;
}else{
newMonth=intMonth;
newDate=intDay;
}
if(newMonth<10)
newMonth="0"+newMonth;
if(newDate<10)
newDate="0"+newDate;
result=newYear+"-"+newMonth+"-"+newDate;
return result;
}function addMonth(months){ //得到与系统时间相差指定月份的时间 传入的参数months就是指定的月份
date=new Date();//系统日期
intYear=date.getYear();
intMonth=date.getMonth()+1;
intDay=date.getDate();
newYear=intYear;
newMonth=intMonth+months;
var newDate=0;
if((newMonth==4||newMonth==6||newMonth==9||newMonth==11)&&intDay==31){
newMonth=newMonth+1;
newDate=1;
}else if(!isLeapYear(intYear)&&newMonth==2&&(intDay==29||intDay==30||intDay==31)){
newMonth=3;
newDate=1;
}else if(isLeapYear(intYear)&&newMonth==2&&(intDay==30||intDay==31)){
newMonth=3;
newDate=1;
}else{
newDate=intDay;
}
if(newMonth/12>0){ //如果月份加了之后超过12,则年份上也要相应增加
newYear=intYear+parseInt(newMonth/12);
newMonth=newMonth%12 ;
}
if(newMonth<10)
newMonth="0"+newMonth;
if(newDate<10)
newDate="0"+newDate;
result=newYear+"-"+newMonth+"-"+newDate;
return result;
}
<script>
//取得明年的今天
function getNextYear(){
var today = new Date();
var y,m,d;
d = today.getDate();
m = today.getMonth() + 1;//月是从0开始
y = today.getYear();
y++;//年加1
if(m==2 && d==29)
return new Date(y,3,1);//如果是闰年,返回3月1日
else
return new Date(y,m,d);//如果是闰年,返回3月1日
}
//取得下个月的今天
function getNextMonth(){
var today = new Date();
var y,m,d;
d = today.getDate();
m = today.getMonth() + 1;//月是从0开始
y = today.getYear();
m++;
if( m > 12){//月份加1,如果大于12,则月份为1份,年份加1
m=1;y++;
}
if( !checkDate(y + '-' + m + '-' + d) ){//如果不是日期,那么月份加1,日等于1
m++;
if( m > 12){//月份加1,如果大于12,则月份为1份,年份加1
m=1;y++;
}
d=1;
}
var nextToday = new Date(y, m, d);
return nextToday;
}
//判断是否正确的日期,格式:年-月-日
function checkDate(str)
{
var tmp = new Date(str);
var y = tmp.getFullYear();
var m = tmp.getMonth()+1;
var d = tmp.getDate();
var nday = y + "-" + m + "-" + d
if (nday != str)
return false;
else
return true;
}</script>
{
var d = new Date();
return new Date(d.getFullYear(),d.getMonth(),d.getDate());
}
不用那么复杂,系统自己会算……
另外输出格式可以自己写一个解析函数,标准的做法是重写toLocaleString()
{
var d = new Date();
return new Date(d.getFullYear()+1,d.getMonth(),d.getDate());
}
不用那么复杂,系统自己会算……
另外输出格式可以自己写一个解析函数,标准的做法是重写toLocaleString()Sorry…… :P
function getNextDay(i){
return new Date(new Date().getTime()+i*1000*86400); //以天计算
}
//函数
Date.prototype.addMonth = function(num){if(!isNaN(num))this.setMonth(this.getMonth() + parseInt(num));return this;}
//创建date
var date = new Date();
date.addMonth(1);//加1个月
date.addMonth(10);//加10个月
date.addMonth(-1);//减1个月//其他函数
Date.prototype.addYear = function(num){if(!isNaN(num))this.setFullYear(this.getFullYear() + parseInt(num));return this;}
Date.prototype.addMonth = function(num){if(!isNaN(num))this.setMonth(this.getMonth() + parseInt(num));return this;}
Date.prototype.addDay = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num));return this;}
Date.prototype.addHour = function(num){if(!isNaN(num))this.setHours(this.getHours() + parseInt(num));return this;}
Date.prototype.addMinute = function(num){if(!isNaN(num))this.setMinutes(this.getMinutes() + parseInt(num));return this;}
Date.prototype.addSecond = function(num){if(!isNaN(num))this.setSeconds(this.getSeconds() + parseInt(num));return this;}
Date.prototype.addMS = function(num){if(!isNaN(num))this.setMilliseconds(this.getMilliseconds() + parseInt(num));return this;}
Date.prototype.addWeek = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num) * 7);return this;}