Sign in Join
FPDoc Onlineexperiment

CompareDate function

In unit DateUtils

Compare 2 dates, disregarding the time of day.

Declaration

pascal
function CompareDate(const A: TDateTime; const B: TDateTime)
: TValueRelationship

Arguments

A

First date to compare.

B

Second date to compare.

Function result

A negative number if date A is before B, zero if they are equal, or positive if A is after B.

Description

CompareDate compares the date parts of two timestamps A and B and returns the following results:

< 0
if the day part of A is earlier than the day part of B.
0
if A and B are the on same day (times may differ) .
> 0
if the day part of A is later than the day part of B.

See also

Example: ex99

pascal
Program Example99;

{ This program demonstrates the CompareDate function }

Uses SysUtils,DateUtils;

Const
  Fmt = 'dddd dd mmmm yyyy';

Procedure Test(D1,D2 : TDateTime);

Var
  Cmp : Integer;

begin
  Write(FormatDateTime(Fmt,D1),' is ');
  Cmp:=CompareDate(D1,D2);
  If Cmp<0 then
    write('earlier than ')
  else if Cmp>0 then
    Write('later than ')
  else
    Write('equal to ');
  Writeln(FormatDateTime(Fmt,D2));
end;

Var
  D,N : TDateTime;

Begin
  D:=Today;
  N:=Now;
  Test(D,D);
  Test(N,N);
  Test(D+1,D);
  Test(D-1,D);
  Test(D+OneSecond,D);
  Test(D-OneSecond,D);
  Test(N+OneSecond,N);
  Test(N-OneSecond,N);
End.

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