Sign in Join
FPDoc Onlineexperiment

CompareTime function

In unit DateUtils

Compares two times of the day, disregarding the date part.

Declaration

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

Arguments

A

First time to compare.

B

Second time to compare.

Function result

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

Description

CompareTime compares the time parts of two timestamps A and B and returns the following results:

< 0
if the time part of A is earlier than the time part of B.
0
if A and B have the same time part (dates may differ) .
> 0
if the time part of A is later than the time part of B.

See also

Example: ex100

pascal
Program Example100;

{ This program demonstrates the CompareTime 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),' has ');
  Cmp:=CompareDateTime(D1,D2);
  If Cmp<0 then
    write('earlier time than ')
  else if Cmp>0 then
    Write('later time than ')
  else
    Write('equal time with ');
  Writeln(FormatDateTime(Fmt,D2));
end;

Var
  D,N : TDateTime;

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

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