Date对象的构造方法就行....new Date(year,month,day,hours,minutes,seconds,milseconds)各个参数,你可以随便的怎么样传入数值型参数,Date对象会自己帮你计算好日期...var dat = new Date();
new Date(dat.getFullYear()-1,dat.getMonth(),dat.getDate());
获得去年的今天这一天的日期...
new Date(dat.getFullYear()-1,dat.getMonth(),dat.getDate());
获得去年的今天这一天的日期...
Date.prototype.addDay=function (num)
{
this.setDate(this.getDate()+num);
return this;
}Date.prototype.addMonth=function (num)
{
var tempDate=this.getDate();
this.setMonth(this.getMonth()+num);
if(tempDate!=this.getDate()) this.setDate(0);
return this;
}Date.prototype.addYear=function (num)
{
var tempDate=this.getDate();
this.setYear(this.getYear()+num);
if(tempDate!=this.getDate()) this.setDate(0);
return this;
}
var d1;
d1=new Date('2004/02/29');
alert(d1.addYear(1));d1=new Date('2004/01/31');
alert(d1.addMonth(1));d1=new Date('2004/02/29');
alert(d1.addDay(1));
</script>
var date = new Date(); var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate(); var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds(); function _f(n){
if(n<10)
n = "0"+n;
return n;
} return year+"-"+_f(month)+"-"+_f(day)+" "+_f(hours)+":"+_f(minutes)+":"+_f(seconds);
}用这段alert(new Date());
结果就是你那样的格式.
如果dat=(2005,01,01)
new Date(dat.getFullYear(),dat.getMonth()+11,dat.getDate());
那么得到的会是2006-0-01了,是不是还要自己认为的判断一下啊?