Sign in Join
FPDoc Onlineexperiment

WithinPastMonths function

In unit DateUtils

Check whether two TDateTimes are only a number of months apart.

Declaration

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

Arguments

ANow

First moment in time.

AThen

Second moment in time.

AMonths

Number of months to check.

Function result

True if ANow and Athen are only AMonths apart, false otherwise.

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.

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

Example: ex48

pascal
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.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.