DateUtils 提供 IncYear/Month/Week/Day/Hour/Minute/Second/MilliSecond 八個函數,統一簽名 function IncXxx(const AValue: TDateTime; const ANumberOfXxx: Int64 = 1): TDateTime,支援正負增量。
IncYear IncMonth IncWeek IncDay
IncHour IncMinute IncSecond IncMilliSecond
uses DateUtils;
procedure TForm1.FormCreate(Sender: TObject);
var t1, t2: TDateTime;
begin
t1 := StrToDateTime('2011-1-1 1:1:1');
t2 := IncYear(t1); ShowMessage(DateTimeToStr(t2)); {2012-1-1 1:01:01}
t2 := IncYear(t1, 3); ShowMessage(DateTimeToStr(t2)); {2014-1-1 1:01:01}
t2 := IncMonth(t1); ShowMessage(DateTimeToStr(t2)); {2011-2-1 1:01:01}
t2 := IncWeek(t1); ShowMessage(DateTimeToStr(t2)); {2011-1-8 1:01:01}
t2 := IncDay(t1); ShowMessage(DateTimeToStr(t2)); {2011-1-2 1:01:01}
t2 := IncHour(t1); ShowMessage(DateTimeToStr(t2)); {2011-1-1 2:01:01}
t2 := IncMinute(t1); ShowMessage(DateTimeToStr(t2)); {2011-1-1 1:02:01}
t2 := IncSecond(t1); ShowMessage(DateTimeToStr(t2)); {2011-1-1 1:01:02}
t2 := IncMilliSecond(t1);
ShowMessage(FormatDateTime('yyyy-m-d h:n:s:zzz', t2)); {2011-1-1 1:01:01:001}
end;
特性:
ANumberOfXxx 可為負數實現減法TDateTime,不修改原值IncMilliSecond)