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

Hi,

Sample script for your requirement

For today it would give 53

var todaydate = new Date();  

//find the year of the current date  
var oneJan =  new Date(todaydate.getFullYear(), 0, 1);   

// calculating number of days in given year before a given date   
var numberOfDays =  Math.floor((todaydate - oneJan) / (24 * 60 * 60 * 1000));   

// adding 1 since to current date and returns value starting from 0   
var result = Math.ceil(( todaydate.getDay() + 1 + numberOfDays) / 7);   

gs.info(result);

Regards
Ankur

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

Michael Storgaa
Tera Expert

I contacted HI Support in this regard. The answer is that this is not a bug.

This is the reply:

"We have got this issue reviewed by Dev and a problem record via PRB1433577 was created.

Unfortunately, the Dev closed the PRB marking it "Working as Expected" and below is the documentation supporting Dev's statement. The logic for calculating the Week Of the year is also explained in the below documentation.

https://developer.servicenow.com/dev.do#!/reference/api/paris/server_legacy/c_GlideDateTimeAPI#r_GDT...

We truly understand the inconvenience our customers may face because of this. But the logic is hardcoded and this has been there for a long time. Changing the calculation will be a mammoth task and will have platform wide impact.

Dev has admitted that the API is not ISO 8601 compliant and it is driven by our own login. They have asked to raise an Enhancement Request so that we can explore the possibility of a new API which is ISO 8601 compliant."

Which of course makes sense. If the ISO standard was not followed it must near impossible at this point to implement it without impacting all customers.

 

But I thought I'd leave this comment here for people to know that this is indeed Working as expected.

Go with JavaScripts Date Object instead if needed.