GetLastDayOfMonth Routines
Returns Last Day of the Month, for a given Date/Time - Time portion preserved.

Unit
QESBPCSDateTime

Overloaded Variants
Function GetLastDayOfMonth(const DT: TDateTime): TDateTime;
Function GetLastDayOfMonth(const Month, Year: Word): TDateTime;
Function GetLastDayOfMonth(const Month, Year: Integer): TDateTime;

Declaration
Function GetLastDayOfMonth(const DT: TDateTime): TDateTime;

Description
Alternatively for a given Month Year.

Parameters
DT Date/Time to process.
Month Month in given year, 1 = Jan, 12 = Dec.
Year 4-digit Year, such as 1999.

Category
Date/Time Arithmetic Routines
Month Based Arithmetic Routines

Implementation

function GetLastDayOfMonth (const DT: TDateTime): TDateTime;
var
     D, M, Y: Integer;
begin
     OptDecodeDateI (DT, Y, M, D);
     case M of
          2:
               begin
                    if IsLeapYear (Y) then
                         D := 29
                    else
                         D := 28;
               end;
          4, 6, 9, 11: D := 30
     else
          D := 31;
     end;
     Result := OptEncodeDateI (Y, M, D) + Frac (DT);
End;

Declaration
Function GetLastDayOfMonth(const Month, Year: Word): TDateTime;

Implementation

function GetLastDayOfMonth (const Month, Year: Word): TDateTime;
var
     D: Word;
begin
     if (Month < 1) or (Month > 12) then
          raise EConvertError.Create (rsInvalidMonth);

     case Month of
          2:
               begin
                    if IsLeapYear (Year) then
                         D := 29
                    else
                         D := 28;
               end;
          4, 6, 9, 11: D := 30
     else
          D := 31;
     end;
     Result := OptEncodeDateW (Year, Month, D);
End;

Declaration
Function GetLastDayOfMonth(const Month, Year: Integer): TDateTime;

Implementation

function GetLastDayOfMonth (const Month, Year: Integer): TDateTime;
var
     D: Integer;
begin
     if (Month < 1) or (Month > 12) then
          raise EConvertError.Create (rsInvalidMonth);

     case Month of
          2:
               begin
                    if IsLeapYear (Year) then
                         D := 29
                    else
                         D := 28;
               end;
          4, 6, 9, 11: D := 30
     else
          D := 31;
     end;
     Result := OptEncodeDateI (Year, Month, D);
End;


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