CompareDateTime、CompareDate、CompareTime 都是對比時間的函數;
CompareDate 只對比日期部分, CompareTime 之對比時間部分.
兩個參數都是 TDateTime 類型, 如果:
前者 = 後者: 返回 0;
前者 > 後者: 返回 1;
前者 < 後者: 返回 -1;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses DateUtils;
procedure TForm1.FormCreate(Sender: TObject);
var
t1,t2: TDateTime;
r1,r2,r3: Integer;
begin
{日期一致、時間不一致}
t1 := StrToDateTime('2009-5-20 1:1:1');
t2 := StrToDateTime('2009-5-20 1:0:0');
r1 := CompareDateTime(t1, t2); //1
r2 := CompareDate(t1, t2); //0
r3 := CompareTime(t1, t2); //1
ShowMessageFmt('%d,%d,%d', [r1,r2,r3]); //1,0,1
{日期不一致、時間一致}
t1 := StrToDateTime('2009-5-20 1:1:1');
t2 := StrToDateTime('2009-5-21 1:1:1');
r1 := CompareDateTime(t1, t2); //-1
r2 := CompareDate(t1, t2); //-1
r3 := CompareTime(t1, t2); //0
ShowMessageFmt('%d,%d,%d', [r1,r2,r3]); //1,0,1
{日期、時間都不一致}
t1 := StrToDateTime('2009-5-19 1:1:1');
t2 := StrToDateTime('2009-5-20 0:0:0');
r1 := CompareDateTime(t1, t2); //-1
r2 := CompareDate(t1, t2); //-1
r3 := CompareTime(t1, t2); //1
ShowMessageFmt('%d,%d,%d', [r1,r2,r3]); //1,0,1
end;
end.