jo..die Berechnung in ein berechnetes Feld des Docs rein, damit das der Ansichtsindexer nicht machen muss:
REM "Welches Feld ist der Datumswert?";
_Datum := Datum;
@If ( _Datum="";""+@Return("");@Success);
REM "Auf welchen Wochentag fällt der 01.01. eines Jahres?";
REM "So = 0, Sa = 6";
NYBias := @Modulo(@Weekday(@Date(@Year(_Datum); 1; 1)) + 6; 7);
REM "Feststellen der vergangenen Tage plus Schaltjahr-Offset";
Days_gone := @Select(@Month(_Datum); 0; 31; 59; 90; 120; 151; 181; 212; 243; 273; 304; 334)
+ @If(
@Modulo(@Year(_Datum); 4) = 0 & @Modulo(@Year(_Datum); 100) != 0 & @Month(_Datum) > 2; 1; 0)
+ @Day(_Datum);
REM "Erste Ermittlung der KW";
Dat_KW := @Integer((Days_gone + @If(NYBias >= 5; NYBias -2; NYBias + 5)) / 7);
REM "Gehören die Tage noch zur letzten KW des vergangenen Jahres?...";
Dat_Year := @Year(_Datum) + @If(Dat_KW = 0; -1; 0);
REM "... dann auch als KW 53 setzen (aber So, den 01.01. als KW 52 berücksichtigen)!";
Dat_KW1 := @If(Dat_KW = 0 & @Weekday(_Datum) != 0; 53 ; Dat_KW);
@If(Dat_KW1 < 10; 0 + Dat_KW1; Dat_KW1)
Oder:
This formula gives the result as
The number of completed weeks as 0 since it lies in the first week of January
2001.
It also gives the day number in the week as 2. Since it is the second day of
the week.
Example 2:
------------
7/25/2001 (This is the given date in mm/dd/yyyy format).
This formula gives the result as
The number of completed weeks as 29 since it lies in the 30th week of 2001.
It also gives the day number in the week as 3. Since it is the third day of
the 30th week.
Code
Year1:=@Year(GivenDate);
stdate:=@TextToTime("1"+"/"+"1"+"/"+@Text(Year1));
diffdates:=(((GivenDate - stdate) / 86400) / 7);
@Prompt([OK];"The number of completed weeks";@Text(@integer(diffdates)));
@prompt([ok];"This is the nth day of the week";@text(@modulo(((GivenDate -
stdate) / 86400);7) + 1))
ODER
Some applications require the computation of the week number in a month in
which a given date falls. For example, Sunday 02 May 1999 is in the second week
while Saturday 01 May 1999 is in the first week.
This formula computes the week number of date value GivenDate:
WeekNumber := @Integer( (@Day(GivenDate) - @WeekDay(GivenDate)+13) /7 )
ODER
REM "First compute week day Mo: 1 till Su: 7, be careful here if your settings are Su: 1 till Sa: 7";
tmpWeekday := 7-@Modulo(8-@Weekday(Datum);7);
REM "Compute Date Offset to Thursday";
tmpOffsetToThursday := tmpWeekday - 4;
REM "the result is a number
between -3 and +3 -> Mo: -3 und Su: +3";
tmpActThursday := @Adjust(Datum; 0; 0; - tmpOffsetToThursday; 0; 0; 0);
REM "Using this offset, compute the date of the corresponding Thursday and the corresponding year -> this gives us the 1.January, to start from, for the computation of the
calendar week";
tmpJan1rstRef := @Date(@Year(tmpActThursday); 1; 1);
REM "Find out the Thursday corresponding to the week of the 1st January computed above.";
tmpOffsetTo1stThu := 7-@Modulo(8-@Weekday(tmpJan1rstRef);7) - 4;
tmp1stThur := @Adjust(tmpJan1rstRef; 0; 0; - tmpOffsetTo1stThu; 0; 0; 0);
REM "If the found Date is in December, then take the date one week after to get the first week of the year.";
@Set("tmp1stThur";
@If(@Month(tmp1stThur)=12;@Adjust(tmp1stThur;0;0;7;0;0;0); tmp1stThur));
REM "Now, we have the Thursday of the week we want to get the number from and the first Thursday of the corresponding year. Just subtract the one from the other and convert from Seconds to days and weeks.";
tmpKW := ((tmpActThursday - tmp1stThur) /(24*3600) / 7) + 1;
tmpKW
________________
REM "And here is the contracted version of the code";
tmpActThursday := @Adjust(Datum; 0; 0; @Modulo(8-@Weekday(Datum);7) - 3; 0; 0; 0);
tmpJan1rstRef := @Date(@Year(tmpActThursday); 1; 1);
tmp1stThur := @Adjust(tmpJan1rstRef; 0; 0;
@Modulo(8-@Weekday(tmpJan1rstRef);7)-3; 0; 0; 0);
tmpKW := (((tmpActThursday - @If(@Month(tmp1stThur)=12;@Adjust(tmp1stThur;0;0;7;0;0;0); tmp1stThur)) /(24*3600) / 7) + 1);
@Text(@Year(tmpJan1rstRef)) + " / " + @If(tmpKW<10;"0";"") + @Text(tmpKW)
ODER
1) paste the following code (find it at the Code section) into PostOpen of the form
2) do have one field named TSWEEK in the form; field type: text computed; in the value of the field type TSWEEK
That's all!
Note 1: this function calculate the week number of Today(); if you want generalize you have to create a parameter in order to pass the value of the date you want.
Note2: this function consider the week start date as Monday; if you want to consider Sunday, you have to change the row: While lunedi<>2 ' domenica=1, lunedi=2 with the row: While lunedi<>1 ' domenica=1, lunedi=2
Code
If source.editmode=False Then Exit Sub
If Trim(source.fieldgettext("TSWeek"))<>"" Then Exit Sub
Dim inizia As Variant
Dim anno As Variant
Dim giorno As Variant
Dim lunedi As Integer
Dim nweek As Integer
giorno=Today()
anno=Year(giorno)
inizia=Datenumber(anno,1,1)
For y%=1 To 54
' trova il primo lunedi dell'anno e in seguito tutti i lunedi
lunedi=Weekday(inizia)
While lunedi<>2 ' domenica=1, lunedi=2
inizia=inizia+1
lunedi=Weekday(inizia)
Wend
If giorno=inizia Then
nweek=y%
If nweek=0 Then nweek=53
Exit For
Elseif giorno nweek=y%-1
If nweek=0 Then nweek=53
Exit For
Else
inizia=inizia+1
End If
Next
Call source.fieldsettext("TSWeek",Str(nweek))