25
Views
12
Comments
Solved
[Extended DateTime] WeekNumber() result incorrect
Question
Forge component by Matthias Preuter
8
Published on 08 Aug 2019
Application Type
Mobile

The service studio version is 11.8.12

The WeekNumber() function return value is a little bit off.

For example, input parameter datein = 2020-11-2, weekrule = 1, firstdayofweek = 1, the return value is 43, but it should be 44 instead. Or datein = 2020-1-6, return value is 52, but it should be week 1. If the datein is Monday, the return value is true value minus 1.

However, trying in Extended DateTime Demo page, the return value is correct.

Why is this happen and how should i solve it, thank you!

Rank: #369
Solution

What I can imagine is the fact that there's a timezone issue at play. Are you by any chance calling this from a Reactive application? (Update: The tag 'Mobile' gave a hint ;-) )

I've tested this in my personal environment and it seems like it's an automatic time converting issue. When I provide 2020-01-06 it returns 52, as you stated. A log in the extension shows that the date has been converted from my timezone (UTC+1) to UTC (or Ireland's timezone, since the AWS server is there?). So in that case, 2020-01-05 23:00:00 is week 52.

Rank: #369

Hi Horace,

Calendar week definition.

It seems like you need the European definition of week number. In this case the week with 4 days in the new year is week 1. This means that WeekRule need to be set to 2:
2 (First Four-day Week): Indicates that the first week of the year is the first week with four or more days before the designated first day of the week.

Goodluck!

Lennart

Rank: #20560

Hi Lennart,

Thanks for your answer. But i confirm that i need WeekRule = 1 that the first full week is week 1, such that 

2020-01-06 is the first day of week 1.

It is strange that it is no problem on the Extended DateTime demo page but got wrong when i use in development.


Rank: #369
Solution

What I can imagine is the fact that there's a timezone issue at play. Are you by any chance calling this from a Reactive application? (Update: The tag 'Mobile' gave a hint ;-) )

I've tested this in my personal environment and it seems like it's an automatic time converting issue. When I provide 2020-01-06 it returns 52, as you stated. A log in the extension shows that the date has been converted from my timezone (UTC+1) to UTC (or Ireland's timezone, since the AWS server is there?). So in that case, 2020-01-05 23:00:00 is week 52.

mvp_badge
MVP
Rank: #15

Lennart is correct. Reactive and mobile when passing datetimes from client to server side always convert the datetimes to UTC. The only way to solving your problem is using dates instead of datetimes.

Regards,

Marcelo

Rank: #369

Hi Marcelo,

The extension is already receiving a date, but it seems like they're handled as datetimes under the hood.

Regards,
Lennart

mvp_badge
MVP
Rank: #2

As far as timezone issues are concerned, conversion to and from UTC are only applied to Date Time variables, not to Date variables. I don't know what the input parameter is, but if it's a Date Time, then conversion applies.

mvp_badge
MVP
Rank: #15

Did a small test to check this out (in attachment). And if you use a date you shouldn't get any errors when using WeekNumber()

Regards,

Marcelo

ExtendedDatetime.oap

Rank: #369

Hi Marcelo,

I tried the exact same thing as a solution for Horace, but I got the same result as he got. Date 06-01-2020 gives week 53 instead of week 1. The variable type is Date. But if I publish your example on my personal environment, it seems to work.

It's not my issue, but I'm now quite curious now why this happens...

WeekNumberTest.oml

mvp_badge
MVP
Rank: #15

In the WeekNumber() input FirstDayofWeek you are using the number 2 which is Tuesday. You should use 1 or 0. For Monday or Sunday respectively.

Regards,

Marcelo

Rank: #369

Ah, that might explain the difference ;-)

Btw, if you don't wrap the extension method in a server action wrapper (which of course is best practice), then it doesn't matter if your variable is Date or DateTime, it'll pass the parameter as DateTime. I think that is the case in Horace's original question.

Rank: #20560

Hi Lennart,

It seems like you are correct. I pass Date type variable to the weekNumber function but maybe it convert it to DateTime and do the timezone conversion. So just as you said i wrap the function in a server action wrapper then the return value is correct now! Thank you for your help sincerely.


Regards,

Horace

Rank: #20560

Hi Marcelo,

You are right. It should be a timezone conversion problem cause by datetime. 

Thank you for your help.

Regards,

Horace