Domino Code Fragment

Code Name*
What week is it?
Date*
04/29/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.221.13.173
Description*
Some applications require calculations based on the week of the year. Here's the ISO standard on week calculations: "Numerical representation of a period of seven calendar days, starting with Monday and numbered from 01 to 53, with
number 01 assigned to the first week containing at least four days of the new year, and preceded by the letter "W" if needed in order to avoid misunderstanding."
Type*
Formula
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:

Here's an @Function to determine the week:

REM "Get the date to convert";
date := @TextToTime(@Prompt([OKCANCELEDIT]; "What week is it?"; "Enter a date:"; @Text(@Today)));


REM "Find the beginning of the week";
wkd1 := @Weekday(date);
WB := @Adjust(date; 0; 0; -@If(wkd = 1; 0; wkd1 - 1); 0; 0; 0);


REM "Find the beginning of the first official week of the year";
jan1 := @Date(@Year(date); 1; 1);
wkd2 := @Weekday(jan1);
tYB := @Adjust(jan1; 0; 0; -@If(wkd2 = 1; 0; wkd2 - 1); 0; 0; 0);
YB := @If(tYB <= @Date(@Year(tYB); 12; 28); @Adjust(tYB; 0; 0; 7; 0; 0; 0); tYB);


REM "Find the beginning of the first official day of last year";
jan1last := @Date(@Year(date) - 1; 1; 1);
wkd3 := @Weekday(jan1last);
tYBlast := @Adjust(jan1last; 0; 0; -@If(wkd3 = 1; 0; wkd3 - 1); 0; 0; 0);
YBlast := @If(tYBlast <= @Date(@Year(tYBlast); 12; 28); @Adjust(tYBlast; 0; 0; 7; 0; 0; 0); tYBlast);


REM "Determine which year to use";
FirstWeek := @If(date < YB; YBlast; YB);


REM "Calculate the week and the year";
Week := @Right("0" + @Text((((WB - FirstWeek) / 86400) / 7) + 1); 2);
Year := @Text(@Year(@Adjust(FirstWeek; 0; 0; 3; 0; 0; 0)));


@Prompt([OK]; "What week is it?"; Year + " - " + Week)