0

I am simply trying to get this function to work when I run it in script editor. Gives the error:

Cannot find function getYear in object [object Object].

I have tried the same with getFullYear but to no avail. Strangely, when I put it inside another larger function that has nothing to do with it, it seems to work perfectly. On its own, it gives that error. Here is the code:

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()
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • The way you are calculating the week of the year is not consistent with ISO 8601. See [*Get week of year in JavaScript like in PHP*](http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php/6117889#6117889). – RobG Jun 15 '16 at 23:13

1 Answers1

0

Use it this way without the new in the return.

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
}
Pedro Estrada
  • 2,384
  • 2
  • 16
  • 24