Sign in Join
FPDoc Onlineexperiment

WithinPastYears function

In unit DateUtils

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

Declaration

pascal
function WithinPastYears(const ANow: TDateTime; const AThen: TDateTime; 
 const AYears: Integer): Boolean

Arguments

ANow

First moment in time.

AThen

Second moment in time.

AYears

Number of years to check.

Function result

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

Description

WithinPastYears compares the timestamps ANow and AThen and returns True if the difference between them is at most AYears years apart, or False if they are further apart.

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

See also

Example: ex47

pascal
Program Example47;

{ This program demonstrates the WithinPastYears function }

Uses SysUtils,DateUtils;

Procedure Test(ANow,AThen : TDateTime; AYears : Integer);

begin
 Write(DateToStr(AThen),' and ',DateToStr(ANow));
 Write(' are within ',AYears,' years: ');
 Writeln(WithinPastYears(ANow,AThen,AYears));
end;

Var
  D1,D2 : TDateTime;

Begin
  D1:=Today;
  D2:=Today-364;
  Test(D1,D2,1);
  D2:=Today-365;
  Test(D1,D2,1);
  D2:=Today-366;
  Test(D1,D2,1);
  D2:=Today-390;
  Test(D1,D2,1);
  D2:=Today-368;
  Test(D1,D2,1);
  D2:=Today-1000;
  Test(D1,D2,1);
  Test(D1,D2,2);
  Test(D1,D2,3);
End.

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