Examples Delphi

Title: Week Number of a given Date
Question: How to calculate the Week Number of a given Date
Answer:
Here is the function :
function Week_Num(Dt: TDateTime): Word; // Week Number of Dt
var DoW,Wn,Y,M,D: Word;
D1: TDateTime;
begin
DecodeDate(Dt,Y,M,D);
D1:= EncodeDate(Y,1,1);
DoW:= DayOfWeek(D1)-1;
Wn:= ((DoW+Trunc(Dt-D1+1)+5) div 7)-(DoW div 5);
if Wn = 53 then
case DayOfWeek(EncodeDate(Y,12,31)) of
2..4: Wn:= 1;
end;
if Wn = 0 then
Wn:= Week_Num(EncodeDate(Y-1,12,31));
Week_Num:= Wn;
end;
{ Exemples : Week_Num(EncodeDate(1999,1,1)) return 53
Week_Num(EncodeDate(2001,1,1)) return 1
Week_Num(EncodeDate(2001,12,30)) return 52
Week_Num(EncodeDate(2001,12,31)) return 1 }