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

  • A+
所属分类:[开发技巧]

CompareDateTime、CompareDate、CompareTime 返回的是 -1、0、1;
前者 = 后者: 返回 0;
前者 > 后者: 返回 1;
前者 < 后者: 返回 -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.

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: