Skip to content

WithinPastMonths

Check whether two TDateTimes are only a number of months apart

Declaration

Source position: dateutil.inc line 244

  function WithinPastMonths(const ANow: TDateTime; 
                           const AThen: TDateTime; const AMonths: Integer)
                            : Boolean;

Description

WithinPastMonths compares the timestamps ANow and AThen and returns True if the difference between them is at most AMonths months apart, or False if they are further apart.

Remark

Since this function uses the MonthsBetween function to calculate the difference in Months, this means that fractional months do not count, and the fractional part is simply dropped, so for two dates actually 2 and a half months apart, the result will also be True !!!

See also

Name Description
WithinPastDays Check whether two TDateTimes are only a number of days apart
WithinPastHours Check whether two TDateTimes are only a number of hours apart
WithinPastMilliSeconds Check whether two TDateTimes are only a number of milliseconds apart
WithinPastMinutes Check whether two TDateTimes are only a number of minutes apart
WithinPastSeconds Check whether two TDateTimes are only a number of seconds apart
WithinPastWeeks Check whether two TDateTimes are only a number of weeks apart
WithinPastYears Check whether two TDateTimes are only a number of years apart

Example

Program Example48;
{ This program demonstrates the WithinPastMonths function }
Uses SysUtils,DateUtils;
Procedure Test(ANow,AThen : TDateTime; AMonths : Integer);
begin
 Write(DateToStr(AThen),' and ',DateToStr(ANow));
 Write(' are within ',AMonths,' months: ');
 Writeln(WithinPastMonths(ANow,AThen,AMonths));
end;
Var
  D1,D2 : TDateTime;
Begin
  D1:=Today;
  D2:=Today-364;
  Test(D1,D2,12);
  D2:=Today-365;
  Test(D1,D2,12);
  D2:=Today-366;
  Test(D1,D2,12);
  D2:=Today-390;
  Test(D1,D2,12);
  D2:=Today-368;
  Test(D1,D2,11);
  D2:=Today-1000;
  Test(D1,D2,31);
  Test(D1,D2,32);
  Test(D1,D2,33);
End.