DateUtils.YearsBetween(); DateUtils.MonthsBetween(); DateUtils.WeeksBetween(); DateUtils.DaysBetween(); DateUtils.HoursBetween(); DateUtils.MinutesBetween(); DateUtils.SecondsBetween(); DateUtils.MilliSecondsBetween(); DateUtils.YearSpan(); DateUtils.MonthSpan(); DateUtils.WeekSpan(); DateUtils.DaySpan(); DateUtils.HourSpan(); DateUtils.MinuteSpan(); DateUtils.SecondSpan(); DateUtils.MilliSecondSpan();
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-9-21 22:33:44'); i := YearsBetween(t1, t2); //11 i := MonthsBetween(t1, t2); //136 i := WeeksBetween(t1, t2); //591 i := DaysBetween(t1, t2); //4142 i := HoursBetween(t1, t2); //99419 i := MinutesBetween(t1, t2); //5965151 i := SecondsBetween(t1, t2); //357909071 i := MilliSecondsBetween(t1, t2); //357909070999 i := YearsBetween(t2, t1); //11, 参数无先后 {下面这个系列返回的时间间隔是 Double 类型的, 更精确} d := YearSpan(t1, t2); //11.3414540712855 d := MonthSpan(t1, t2); //136.097448855426 d := WeekSpan(t1, t2); //591.780871362434 d := DaySpan(t1, t2); //4142.46609953704 d := HourSpan(t1, t2); //99419.1863888889 d := MinuteSpan(t1, t2); //5965151.18333333 d := SecondSpan(t1, t2); //357909071 d := MilliSecondSpan(t1, t2); //357909071000 end; end.
DateUtils-Function