javascript日期对象:使用addDays函数时出错

时间:2009-10-26 11:27:50

标签: javascript datetime

我试图在我的javascript中执行以下代码并得到一些异常

 var now3 = new Date();
 now3.addDays(-4);

抛出的错误是 Microsoft JScript运行时错误:对象不支持此属性或方法

我在我的页面中使用了jQuery 1.3.2。

这有什么不对吗?请帮忙

4 个答案:

答案 0 :(得分:11)

没有addDays()方法 - 您需要使用setDate()

now3.setDate(now3.getDate() - 4);

有关详细信息,请参阅Date object documentation

答案 1 :(得分:3)

好吧,你可以将addDays添加到Date原型中:

Date.prototype.addDays = function(days) {
  this.setDate( this.getDate()  + days);
  return this;
};

这应该可以正常工作。

答案 2 :(得分:0)

Greg的提示是要走的路。您可以在此处找到有关JavaScript日期操作的更多信息:http://www.w3schools.com/js/js_obj_date.asp

答案 3 :(得分:0)

now3.addDays(-4);

把这个功能

Date.prototype.addDays = function(s) 
        { 

          var targetDays = parseInt(s) 
          var thisYear = parseInt(this.getFullYear()) 
          var thisDays = parseInt(this.getDate()) 
          var thisMonth = parseInt(this.getMonth() + 1) 

          var currDays = thisDays; 
          var currMonth = thisMonth; 
          var currYear = thisYear; 

          var monthArr; 

          var nonleap = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];   
          // leap year  
          var leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];   

          if ((thisYear % 4) == 0) {     
            if ((thisYear % 100) == 0 && (thisYear % 400) != 0) { monthArr = nonleap; }  
            else { monthArr = leap; }   
          }   
          else {  monthArr = nonleap; }  

          var daysCounter = 0; 
          var numDays = 0; 
          var monthDays = 0; 

          if( targetDays < 0) { 

            while(daysCounter < (targetDays * -1) ) { 

              if(daysCounter == 0) { 
                if((targetDays * -1) < thisDays) { 
                  break; 
                } else { 
                  daysCounter = thisDays; 
                } 
              }else { 
                numDays = monthArr[currMonth - 1]; 
                daysCounter += parseInt(numDays) 
              } 

              if(daysCounter > (targetDays * -1) ) { 
                break; 
              } 

              currMonth = currMonth - 1; 

              if(currMonth == 0) { 
                currYear = currYear - 1; 
                if ((currYear % 4) == 0) {  
                  if ((currYear % 100) == 0 && (currYear % 400) != 0) { monthArr = nonleap; }  
                  else { monthArr = leap; }   
                }   
                else {  monthArr = nonleap; }  
                currMonth = 12; 
              }   
            } 

            t = this.getTime(); 
            t += (targetDays * 86400000); 
            this.setTime(t) 
            var thisDate = new Date(currYear,currMonth - 1,this.getDate()) 
            return thisDate; 

          } else { 

            var diffDays = monthArr[currMonth - 1] - thisDays; 

            numDays = 0; 
            var startedC = true; 

            while(daysCounter < targetDays  ) { 

              if(daysCounter == 0 && startedC == true) { 
                monthDays = thisDays; 
                startedC = false; 
              }else { 
               monthDays++; 
               daysCounter++; 

                if(monthDays > monthArr[currMonth - 1]){ 
                  currMonth = currMonth + 1; 
                  monthDays = 1; 
                } 

              } 

              if(daysCounter > targetDays) { 
                break; 
              } 

              if(currMonth == 13) { 
                currYear = currYear + 1; 
                if ((currYear % 4) == 0) {  
                  if ((currYear % 100) == 0 && (currYear % 400) != 0) { monthArr = nonleap; }  
                  else { monthArr = leap; }   
                }   
                else {  monthArr = nonleap; }  
                currMonth = 1; 
              }   
            } 

            var thisDate = new Date(currYear,currMonth - 1,monthDays) 
            return thisDate; 
          } 
        }