Date2WeekNo Function
Converts Dates into a Week No in the Current Year.

Unit
QESBPCSDateTime

Declaration
Function Date2WeekNo(const DT: TDateTime): Integer;

Description
Weeks are assumed to start with Sunday. The week that Jan 1 occurs is the 1st week of the year, the Sunday AFTER Jan 1 would be the start of the 2nd week of the year. Note that this does mean that there can be 54 weeks in a year!

Parameters
DT Date/Time to be processed.

Category
Date/Time Arithmetic Routines
Week Based Arithmetic Routines

Implementation

function Date2WeekNo (const DT: TDateTime): Integer;
var
     Year: Integer;
     FirstSunday, StartYear: TDateTime;
     WeekOfs: Byte;
begin
     Year := OptDate2Year (DT);
     StartYear := GetFirstDayOfYear (Year);
     if DayOfWeek (StartYear) = 0 then
     begin
          FirstSunday := StartYear;
          WeekOfs := 1;
     end
     else
     begin
          FirstSunday := StartOfWeek (StartYear) + 7;
          WeekOfs := 2;
          if DT < FirstSunday then
          begin
               Result := 1;
               Exit;
          end;
     end;
     Result := DaysApart (FirstSunday, StartofWeek (DT)) div 7 + WeekOfs;
End;


HTML generated by Time2HELP
http://www.time2help.com