| AgeAtDateInMonths Function | 
Unit
QESBPCSDateTime
Declaration
Function AgeAtDateInMonths(const DOB, DT: TDateTime): Integer;
Description
If DT occurs before DB then -1 is returned. Routine donated by David Gobbett.
| Parameters | 
| DOB | Date of Birth. | 
| DT | Date in question. | 
Returns
Age in Integral Months at the Date in question.
Category
Date/Time Arithmetic Routines
Month Based Arithmetic Routines
Year Based Arithmetic RoutinesImplementation
 
| function AgeAtDateInMonths (const DOB, DT: TDateTime): Integer;
var
     D1, D2: Integer;
     M1, M2: Integer;
     Y1, Y2: Integer;
begin
     if DT < DOB then
          Result := -1
     else
     begin
          OptDecodeDateI (DOB, Y1, M1, D1);
          OptDecodeDateI (DT, Y2, M2, D2);
          if Y1 = Y2 then // Same Year
               Result := M2 - M1
          else // Different Years
          begin
               // 12 months per year age
               Result := 12 * AgeAtDate (DOB, DT);
               if M1 > M2 then
                    Result := Result + (12 - M1) + M2
               else if M1 < M2 then
                    Result := Result + M2 - M1
               else if D1 > D2 then // Same Month
                    Result := Result + 12;
          end;
          if D1 > D2 then // we have counted one month too many
               Dec (Result);
     end;
End; | 
|  |