DayOfMonth2Date Routines
Returns the Given Occurrence (Day of Month) of a Day of Week in a given Month/Year.

Unit
QESBPCSDateTime

Overloaded Variants
Function DayOfMonth2Date(const DOMType: TESBDOMType; const DOW: Byte; const Month, Year: Word): TDateTime;
Function DayOfMonth2Date(const DOMType: TESBDOMType; const DOW, Month, Year: Integer): TDateTime;

Declaration
Function DayOfMonth2Date(const DOMType: TESBDOMType; const DOW: Byte; const Month, Year: Word): TDateTime;

Description
Thus can be used to find the first Wednesday, Last Monday, etc. DOMType can be one of the following:

domFirst - First occurrence in a Month.

domSecond - Second occurrence in a Month.

domThird - Third occurrence in a Month.

domFourth - Fourth occurrence in a Month.

domLast - Last occurrence in a Month.

Parameters
DOMType the desired Day of Month Type.
DOW the Day of Week, 1 = Sunday, 7 = Saturady.
Month the month of the year, 1 = Jan, 12 = Dec.
Year 4-digit year such as 1999.

Returns
the Date uniquely defined by the above.

Category
Date/Time Arithmetic Routines
Month Based Arithmetic Routines

Implementation

function DayOfMonth2Date (const DOMType: TESBDOMType; const DOW: Byte;
     const Month, Year: Word): TDateTime;
var
     Ofs: Integer;
     DT: TDateTime;
begin
     if (DOW < 1) or (DOW > 7) then
          raise EConvertError.Create (rsInvalidDOW);
     if (Month < 1) or (Month > 12) then
          raise EConvertError.Create (rsInvalidMonth);

     if DOMType < domLast then
     begin
          DT := GetFirstDayOfMonth (Month, Year);
          Ofs := DOW - DayOfWeek (DT);
          if Ofs < 0 then
               Ofs := Ofs + 7;
          Result := DT + Ofs + 7 * Integer (DOMType);
     end
     else
     begin
          DT := GetLastDayOfMonth (Month, Year);
          Ofs := DayofWeek (DT) - DOW;
          if Ofs < 0 then
               Ofs := Ofs + 7;
          Result := DT - Ofs;
     end;
End;

Declaration
Function DayOfMonth2Date(const DOMType: TESBDOMType; const DOW, Month, Year: Integer): TDateTime;

Implementation

function DayOfMonth2Date (const DOMType: TESBDOMType;
     const DOW, Month, Year: Integer): TDateTime;
var
     Ofs: Integer;
     DT: TDateTime;
begin
     if (DOW < 1) or (DOW > 7) then
          raise EConvertError.Create (rsInvalidDOW);
     if (Month < 1) or (Month > 12) then
          raise EConvertError.Create (rsInvalidMonth);

     if DOMType < domLast then
     begin
          DT := GetFirstDayOfMonth (Month, Year);
          Ofs := DOW - DayOfWeek (DT);
          if Ofs < 0 then
               Ofs := Ofs + 7;
          Result := DT + Ofs + 7 * Integer (DOMType);
     end
     else
     begin
          DT := GetLastDayOfMonth (Month, Year);
          Ofs := DayofWeek (DT) - DOW;
          if Ofs < 0 then
               Ofs := Ofs + 7;
          Result := DT - Ofs;
     end;
End;


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