DOWsInRange Routines
Returns the number of the specified Day of Week in the Given Date Range (inclusive).

Unit
QESBPCSDateTime

Overloaded Variants
Function DOWsInRange(const DOW: Byte; const StartDT, EndDT: TDateTime): Integer;
Function DOWsInRange(const DOW: TESBDayofWeek; const StartDT, EndDT: TDateTime): Integer;

Declaration
Function DOWsInRange(const DOW: Byte; const StartDT, EndDT: TDateTime): Integer;

Description
If EndDT occurs before StartDT then they are swapped. Thanks to Tom Grieve for his assistance with this routine. Exception results for invalid DOW or invalid Month.

Parameters
DOW Day of the Week, Sunday = 1 through Saturday = 7. Overloaded version also allows for TESBDayOfWeek type
StartDT The beginning Date
EndDT The end Date

Category
Date/Time Arithmetic Routines

Implementation

function DOWsInRange (const DOW: Byte; const StartDT, EndDT: TDateTime): Integer;
var
     DT1, DT2: TDateTime;
     StartDOW, Days, Weeks, Extras: Integer;
begin
     if (DOW < 1) or (DOW > 7) then
          raise EConvertError.Create (rsInvalidDOW);

     DT1 := StartDT;
     DT2 := EndDT;
     if DT2 < DT1 then
          SwapXY (DT1, DT2);
     Days := Trunc (DT2) - Trunc (DT1) + 1;
     Weeks := Days div 7;
     Extras := Days mod 7;
     StartDOW := DayOfWeek (DT1);

     if (Integer (DOW) - StartDOW + 7) mod 7 < Extras then
          Result := Weeks + 1
     else
          Result := Weeks;
End;

Declaration
Function DOWsInRange(const DOW: TESBDayofWeek; const StartDT, EndDT: TDateTime): Integer;

Implementation

function DOWsInRange (const DOW: TESBDayofWeek; const StartDT, EndDT: TDateTime): Integer;
begin
     Result := DOWsInRange (Integer (DOW) + 1, StartDT, EndDT);
End;


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