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

And that's exactly my issue - that ServiceNow disagrees with what I would consider a standard. So next week is week 2 in ServiceNow and week 1 everywhere else? 

That's not good.

Hi,

if you are using ServiceNow's API then that is how it is defined to give you the output

Regards
Ankur

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

Hi,

This is my interpretations as per docs

This week starts on 27th December and ends on 2nd Jan 2021

So if in this week we check the week number comes as 1st since this week has the 1st 2 days of new year 2021.

Also as per docs it states the highest week number in a year is either 52 or 53.

So you get 53 as the week number when you use this date as it doesn't have any of the new year's date in it

var gdt = new GlideDateTime('2016-12-27');

gs.info(gdt.getWeekOfYearLocalTime()); -> gives 53

find_real_file.png

Regards
Ankur

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

Exactly. Servicenow just ignores any ISO standards and prints the week wrong.
Week 1 is always the holder of 4th january.

Thank you Ankur - I somehow missed that description in the docs, even if I don't agree with why they decided to make it work that way (different than everywhere else, it appears).