Sign in Join
FPDoc Onlineexperiment

CompareDateTime function

In unit DateUtils

Compare 2 dates, taking into account the time of day.

Declaration

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

Arguments

A

First date-time to compare.

B

First date-time to compare.

Function result

A negative number if A is earlier than B, zero if they are equal, or positive if A is later than B.

Description

CompareDateTime compares two timestamps A and B and returns the following results:

< 0
if A is earlier in date/time than B.
0
if A and B are the same date/time .
> 0
if A is later in date/time than B.

See also

Example: ex98

pascal
Program Example98;

{ This program demonstrates the CompareDateTime function }

Uses SysUtils,DateUtils;

Const
  Fmt = 'dddd dd mmmm yyyy hh:nn:ss.zzz';

Procedure Test(D1,D2 : TDateTime);

Var
  Cmp : Integer;

begin
  Write(FormatDateTime(Fmt,D1),' is ');
  Cmp:=CompareDateTime(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.