YearSpan(DateTime1, DateTime2) //返回DateTime1, DateTime2的年间隔
MonthSpan(DateTime1, DateTime2) //返回DateTime1, DateTime2的月间隔
WeekSpan(DateTime1, DateTime2) //返回DateTime1, DateTime2的星期间隔
DaySpan(DateTime1, DateTime2) //返回DateTime1, DateTime2的天间隔
HourSpan(DateTime1, DateTime2) //返回DateTime1, DateTime2的小时间隔
MinuteSpan(DateTime1, DateTime2) //返回DateTime1, DateTime2的分钟间隔
SecondSpan(DateTime1, DateTime2) //返回DateTime1, DateTime2的秒间隔
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;
i: Int64;
d: Double;
begin
t1 := StrToDateTime('2009-5-20 11:22:33');
t2 := StrToDateTime('2020-5-20 11:22:33');
i := YearsBetween(t1, t2); //11
i := MonthsBetween(t1, t2); //132
i := WeeksBetween(t1, t2); //574
i := DaysBetween(t1, t2); //4018
i := HoursBetween(t1, t2); //96432
i := MinutesBetween(t1, t2); //5785920
i := SecondsBetween(t1, t2); //347155200
i := MilliSecondsBetween(t1, t2); //347155200000
i := YearsBetween(t2, t1); //11
{下面这个系列返回的时间间隔是 Double 类型的, 更精确}
d := YearSpan(t1, t2); //11.0006844626968
d := MonthSpan(t1, t2); //132.008213552361
d := WeekSpan(t1, t2); //574.00
d := DaySpan(t1, t2); //4018.00
d := HourSpan(t1, t2); //96432.00
d := MinuteSpan(t1, t2); //5785920.00
d := SecondSpan(t1, t2); //347155200.00
d := MilliSecondSpan(t1, t2); //347155200000.00
end;
end.