Date对象的构造方法就行....new Date(year,month,day,hours,minutes,seconds,milseconds)各个参数,你可以随便的怎么样传入数值型参数,Date对象会自己帮你计算好日期...var dat = new Date();
new Date(dat.getFullYear()-1,dat.getMonth(),dat.getDate());
获得去年的今天这一天的日期...

解决方案 »

  1.   

    <script >
    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>
      

  2.   

    谢谢,可是alert(d1.addYear(1))出来的格式是MON Feb 28 00:00:00UTC+0800 2005,我想得到的格式是2005-09-09,怎么才能变成这种格式呢
      

  3.   

    倒,你使用addYear()之后,得到一个新的Date对象实例,你就不能自己去提取出来以组合成那样的吗??Date.prototype.toString=function(){
    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());
    结果就是你那样的格式.
      

  4.   

    呵呵,谢谢你耐心的讲解
    如果dat=(2005,01,01)
    new Date(dat.getFullYear(),dat.getMonth()+11,dat.getDate());
    那么得到的会是2006-0-01了,是不是还要自己认为的判断一下啊?
      

  5.   

    我那里是重写了Date对象的toString方法,这个方法可以是返回对象的默认值的,我那是让一个Date对象实例的默认值返回一个你想要的那个格式的一个字符串.你自己试一下就知道什么样的效果了...