# CompareDateTime、CompareDate、CompareTime、SameDateTime、SameDate、SameTime - 对比时间的函数

CompareDateTime、CompareDate、CompareTime 返回的是 -1、0、1;

SameDateTime、SameDate、SameTime 返回的是 True 和 False.

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;
b1,b2,b3: Boolean;
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

b1 := SameDateTime(t1, t2);    //False
b2 := SameDate(t1, t2);        //True
b3 := SameTime(t1, t2);        //False

{日期不一致、时间一致}
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

b1 := SameDateTime(t1, t2);    //False
b2 := SameDate(t1, t2);        //False
b3 := SameTime(t1, t2);        //True

{日期、时间都不一致}
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

b1 := SameDateTime(t1, t2);    //False
b2 := SameDate(t1, t2);        //False
b3 := SameTime(t1, t2);        //False
end;

end.


DateUtils-Function
posted on 2009-05-20 10:30  万一  阅读(...)  评论(...编辑  收藏