Page 1 of 1

svyDateUtils week notations

PostPosted: Fri Jul 09, 2021 2:41 pm
by jdbruijn
I am having an issue with the dateutils, with regards to the week calculations.
Which locale is this module using? Is this only dependent on the server locale or also on the client pc locale?
To me, it looks like it solely depents on the server locale.
I have a test server that is giving me unexpected results for the following code:
var dt = new scopes.svyDateUtils.DateTime();
plugins.webnotificationsToastr.info('Date: '+dt.toStartOfDay().toFirstDayOfWeek().addWeeks(-1).date.toISOString()+
' Week: '+scopes.svyDateUtils.getWeekOfYear(dt.date));
When I run this locally on my laptop in developer I get:
Date: 2021-06-27T22:00:00.000Z Week: 26

But on this test server I get:
Date: 2021-06-19T22:00:00.000Z Week: 25

The region settings of this server look the same as on my laptop:
Country or Region: Netherlands
Regional format: Dutch
Timezone: (UTC+01:00) Amsterdam

These time functions will also be used in API calls so what I would like to do, is to force them all to use the ISO standard which is included in ISO 8601. This system dictates that each week begins on a Monday and is associated with the year that contains that week's Thursday.
Hoewever I don’t think this is supported by svyDateUtils. So I am leaning to modify all java.util.Calendar.getInstance() to java.util.Calendar.getInstance(java.util.Locale.UK)
Or do you have a better suggestion for me?

Re: svyDateUtils week notations

PostPosted: Fri Jul 09, 2021 3:44 pm
by paronne
Hi Jos,

indeed the locale used for week calculations depends on the Server's locale (i assume you are running NGClient :wink: ).
There is very little support from the svyDateUtils in calculating week's number using different locales.

The only available signature is
Code: Select all
getDayOfWeek(date, useISO8601)


There is nothing for getWeekOfYear(date).
A possible idea can be to use the Time class instead of the Calendar class.
Maybe using the signatures below, since It may offer all the flexibility a solution may require ( e.g. use server defaults, force ISO, !ISO country, use logged user's locale )

getDayOfWeek(date, country, language)
getWeekOfYear(date, country, language)

Code: Select all
function getWeekOfYear(date, country, language) {
   var localDate = java.time.LocalDate.of(date.getFullYear(), date.getMonth() + 1, date.getDate());
   if (country && language) {
      return localDate.get(java.time.temporal.WeekFields.of(new java.util.Locale(country, language)).weekOfWeekBasedYear());
   } else {
      return localDate.get(java.time.temporal.WeekFields.of(java.util.Locale.getDefault()).weekOfWeekBasedYear());
   }
}



Feel free to push a pull-request in our open source svyDateUtils. Happy to have a look at it.

Regards,
Paolo