getWeekOfYearLocalTime() returning week number of 1 instead of 53

wpatrickhames
Tera Guru

As stated in the subject, I have a script to get the week number and I'm expecting a 53 for this week - however, it's returning a 1. Has anybody else noticed this? I'm thinking it's a bug based on this wiki article:

https://en.wikipedia.org/wiki/ISO_week_date

The script I am using is the following:

var month = new GlideDateTime().getMonth();
var year = new GlideDateTime().getYear();
var weekNum = new GlideDateTime().getWeekOfYearLocalTime();

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi,

yes, it seems to be bug.

Outside of ServiceNow I always have used the following extension of the JavaScript Date object. And it also works fine at ServiceNow (it returns "53")

if (!Date.prototype.getRealYear) {
  Date.prototype.getRealYear = function() {
    return (1900 > this.getYear()) ? this.getYear() + 1900 : this.getYear();
  }
}


if (!Date.prototype.getWeekOfYear) {
  Date.prototype.getWeekOfYear = function() {
    var _tmpDateObj1 = new Date(this.getRealYear(), this.getMonth(), this.getDate(), 0, 0, 1);
    var _dayOfWeek   = (_tmpDateObj1.getDay() == 0) ? 7 : _tmpDateObj1.getDay();

    _tmpDateObj1.setTime(Number(_tmpDateObj1) + (Date.UTC(_tmpDateObj1.getRealYear(), _tmpDateObj1.getMonth(), _tmpDateObj1.getDate(), 0, new Date(2004, 0, 1).getTimezoneOffset(), 1) - Number(_tmpDateObj1)) - (_dayOfWeek - 1) * 86400000);

    if (new Date(_tmpDateObj1.getRealYear(), 11, 29) > _tmpDateObj1) {
      var _tmpDateObj2 = new Date(_tmpDateObj1.getRealYear(), 0, 1);

      _tmpDateObj2 = new Date(Number(_tmpDateObj2) + 86400000 * (8 - _tmpDateObj2.getDay()));

      if (_tmpDateObj2.getDate() > 4) 
        _tmpDateObj2.setTime(Number(_tmpDateObj2) - 604800000);

      return Math.ceil((_tmpDateObj1.getTime() - _tmpDateObj2) / 604800000);
    }
    else {
     return 1;
    }
  }
}

gs.info((new Date()).getWeekOfYear());

View solution in original post

11 REPLIES 11

Maik Skoddow
Tera Patron
Tera Patron

Hi,

yes, it seems to be bug.

Outside of ServiceNow I always have used the following extension of the JavaScript Date object. And it also works fine at ServiceNow (it returns "53")

if (!Date.prototype.getRealYear) {
  Date.prototype.getRealYear = function() {
    return (1900 > this.getYear()) ? this.getYear() + 1900 : this.getYear();
  }
}


if (!Date.prototype.getWeekOfYear) {
  Date.prototype.getWeekOfYear = function() {
    var _tmpDateObj1 = new Date(this.getRealYear(), this.getMonth(), this.getDate(), 0, 0, 1);
    var _dayOfWeek   = (_tmpDateObj1.getDay() == 0) ? 7 : _tmpDateObj1.getDay();

    _tmpDateObj1.setTime(Number(_tmpDateObj1) + (Date.UTC(_tmpDateObj1.getRealYear(), _tmpDateObj1.getMonth(), _tmpDateObj1.getDate(), 0, new Date(2004, 0, 1).getTimezoneOffset(), 1) - Number(_tmpDateObj1)) - (_dayOfWeek - 1) * 86400000);

    if (new Date(_tmpDateObj1.getRealYear(), 11, 29) > _tmpDateObj1) {
      var _tmpDateObj2 = new Date(_tmpDateObj1.getRealYear(), 0, 1);

      _tmpDateObj2 = new Date(Number(_tmpDateObj2) + 86400000 * (8 - _tmpDateObj2.getDay()));

      if (_tmpDateObj2.getDate() > 4) 
        _tmpDateObj2.setTime(Number(_tmpDateObj2) - 604800000);

      return Math.ceil((_tmpDateObj1.getTime() - _tmpDateObj2) / 604800000);
    }
    else {
     return 1;
    }
  }
}

gs.info((new Date()).getWeekOfYear());

Thank you Maik - I was hoping someone viewed it the way that I was. And I appreciate the code - I might just use it until they decide to make it match everywhere else.  

Ankur Bawiskar
Tera Patron
Tera Patron

@wpatrickhames 

If you refer the docs for this it states

-> The first week of the year is the week that contains at least one day of the new year.

So it is working as expected as this current week has 1st jan and 2nd jan in the week

var gdt = new GlideDateTime();
gs.info(gdt.getWeekOfYearLocalTime());

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

But Outlook and Google Calendar disagree 🙂

find_real_file.png  find_real_file.png