- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-30-2020 12:25 PM
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();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 04:10 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 04:10 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 05:04 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 04:37 AM
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());
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2020 04:42 AM