2014-04-05 46 views
0

爲什麼日期和時間不適用於此語法?日期對象toLocaleString不能正常工作

<script> 

    Date.prototype.addHours = function (h) { 
     this.setHours(this.getHours() + h); 
     return this; 
    }; 

    $(document).ready(function() { 
     $("#countdown").countdown({ 
      date: Date.addHours(1).toLocaleString(), 
      format: "on" 
      }, 

      function() { 

       // callback function 
      }); 
    }); 

</script> 

但這完全正常工作:

<script> 

    Date.prototype.addHours = function (h) { 
     this.setHours(this.getHours() + h); 
     return this; 
    }; 

    $(document).ready(function() { 
     $("#countdown").countdown({ 
      date: "4/5/2014 12:13:16 PM", 
      format: "on" 
      }, 

      function() { 

       // callback function 
      }); 
    }); 

</script> 

的變化是關於date屬性。 FYI

+0

因爲'日期'沒有'addHours'方法。只有*實例*的'日期'做。要了解有關原型繼承的更多信息,請參閱https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain –

回答

1

Date是構造函數,你需要的,如果

$("#countdown").countdown({ 
    date: (new Date()).addHours(1).toLocaleString(), 
    format: "on" 
    }, 

不知道這是相當重要的new實例,但請注意,並非所有用戶都會得到相同的字符串返回從toLocaleString()