无法'找到'函数getYear

时间:2016-06-15 21:22:24

标签: javascript date

当我在脚本编辑器中运行它时,我只是想让这个函数工作。给出错误:

Cannot find function getYear in object [object Object].

我和getFullYear尝试过相同但无济于事。奇怪的是,当我把它放在另一个与它无关的大型函数中时,它似乎完美无缺。就其本身而言,它会给出错误。这是代码:

function WeekNumber() {

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getYear(),0,1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}

var currentTime = new Date();
return new currentTime.getWeek()
}

1 个答案:

答案 0 :(得分:0)

以这种方式使用它而不返回new

function WeekNumber() {

  Date.prototype.getWeek = function() {
    // i think you need to use getFullYear here to get the 4 digits of the year.
    var onejan = new Date(this.getFullYear(),0,1); 
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
  }

  var currentTime = new Date();
  //return new currentTime.getWeek() // before
  return currentTime.getWeek() // after
}