[Overview][Constants][Procedures and functions][Index] | Reference for unit 'DateUtils' (#rtl)

# WithinPastYears

Check whether two TDateTimes are only a number of years apart

## Declaration

Source position: dateutil.inc line 256

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

Remark: | 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

WithinPastMonths |  | Check whether two TDateTimes are only a number of months apart
WithinPastWeeks |  | Check whether two TDateTimes are only a number of weeks apart
WithinPastDays |  | Check whether two TDateTimes are only a number of days apart
WithinPastHours |  | Check whether two TDateTimes are only a number of hours 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
WithinPastMilliSeconds |  | Check whether two TDateTimes are only a number of milliseconds apart

## Example

    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.

Documentation generated on: May 14 2021