type
TDayTable = array [1..12] of Word;
PDayTable = ^TDayTable;function IsLeapYear(Year: Word): Boolean;
begin
Result := (Year mod 4 = 0) and
((Year mod 100 <> 0) or
(Year mod 400 = 0));
end;function GetDayTable(Year: Word): PDayTable;
const
DayTable1: TDayTable =
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
DayTable2: TDayTable =
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
DayTables: array[Boolean] of PDayTable = (@DayTable1, @DayTable2);
begin
Result := DayTables[IsLeapYear(Year)];
end;function DaysInMonth(Year, Month: Word): Word;
begin
Result := GetDayTable(Year)[Month];
end;function Month(Date: TDateTime): Word;
var
D, M, Y: Word;
begin
DecodeDate(Date, Y, M, D);
Result := M;
end;function Year(Date: TDateTime): Word;
var
D, M, Y: Word;
begin
DecodeDate(Date, Y, M, D);
Result := Y;
end;function AddDays(Date: TDateTime; Days: Word): TDateTime;
begin
Result := Date + Days;
end;function AddMonths(Date: TDateTime; Months: Word): TDateTime;
var
TotalDays, CurrentMonth, CurrentYear: Word;
begin
TotalDays := 0;
CurrentMonth := Month(Date);
CurrentYear := Year(Date);
while Months > 0 do
begin
Inc(TotalDays, DaysInMonth(CurrentYear, CurrentMonth));
if CurrentMonth = 12 then
begin
CurrentMonth := 1;
Inc(CurrentYear);
end
else
Inc(CurrentMonth);
Dec(Months);
end;
Result := Date + TotalDays;
end;
function AddYears(Date: TDateTime; Years: Word): TDateTime;
var
TotalDays, CurrentYear: Word;
begin
TotalDays := 0;
CurrentYear := Year(Date);
while Years > 0 do
begin
if IsLeapYear(Year(CurrentYear)) then Inc(TotalDays, 366) else
Inc(TotalDays, 365);
Inc(CurrentYear);
Dec(Years);
end;
Result := Date + TotalDays;
end;
function DateAdd(Date: TDateTime; Y, M, D: Word): TDateTime;
begin
Result := AddYears(AddMonths(AddDays(Date, D), M), Y);
end;
TDayTable = array [1..12] of Word;
PDayTable = ^TDayTable;function IsLeapYear(Year: Word): Boolean;
begin
Result := (Year mod 4 = 0) and
((Year mod 100 <> 0) or
(Year mod 400 = 0));
end;function GetDayTable(Year: Word): PDayTable;
const
DayTable1: TDayTable =
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
DayTable2: TDayTable =
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
DayTables: array[Boolean] of PDayTable = (@DayTable1, @DayTable2);
begin
Result := DayTables[IsLeapYear(Year)];
end;function DaysInMonth(Year, Month: Word): Word;
begin
Result := GetDayTable(Year)[Month];
end;function Month(Date: TDateTime): Word;
var
D, M, Y: Word;
begin
DecodeDate(Date, Y, M, D);
Result := M;
end;function Year(Date: TDateTime): Word;
var
D, M, Y: Word;
begin
DecodeDate(Date, Y, M, D);
Result := Y;
end;function AddDays(Date: TDateTime; Days: Word): TDateTime;
begin
Result := Date + Days;
end;function AddMonths(Date: TDateTime; Months: Word): TDateTime;
var
TotalDays, CurrentMonth, CurrentYear: Word;
begin
TotalDays := 0;
CurrentMonth := Month(Date);
CurrentYear := Year(Date);
while Months > 0 do
begin
Inc(TotalDays, DaysInMonth(CurrentYear, CurrentMonth));
if CurrentMonth = 12 then
begin
CurrentMonth := 1;
Inc(CurrentYear);
end
else
Inc(CurrentMonth);
Dec(Months);
end;
Result := Date + TotalDays;
end;
function AddYears(Date: TDateTime; Years: Word): TDateTime;
var
TotalDays, CurrentYear: Word;
begin
TotalDays := 0;
CurrentYear := Year(Date);
while Years > 0 do
begin
if IsLeapYear(Year(CurrentYear)) then Inc(TotalDays, 366) else
Inc(TotalDays, 365);
Inc(CurrentYear);
Dec(Years);
end;
Result := Date + TotalDays;
end;
function DateAdd(Date: TDateTime; Y, M, D: Word): TDateTime;
begin
Result := AddYears(AddMonths(AddDays(Date, D), M), Y);
end;
incmonth(strtodate('2001-01-01'),1)就是2001-02-01日
incmonth(strtodate('2001-01-01'),1*12)就是2002-01-01日
incmonth(strtodate('2001-01-01'),1)就是2001-02-01日
incmonth(strtodate('2001-01-01'),1*12)就是2002-01-01日
{ Increment/decrement datetime fields }function IncYear(const AValue: TDateTime;
const ANumberOfYears: Integer = 1): TDateTime;
// function IncMonth is in SysUtils
function IncWeek(const AValue: TDateTime;
const ANumberOfWeeks: Integer = 1): TDateTime;
function IncDay(const AValue: TDateTime;
const ANumberOfDays: Integer = 1): TDateTime;
function IncHour(const AValue: TDateTime;
const ANumberOfHours: Int64 = 1): TDateTime;
function IncMinute(const AValue: TDateTime;
const ANumberOfMinutes: Int64 = 1): TDateTime;
function IncSecond(const AValue: TDateTime;
const ANumberOfSeconds: Int64 = 1): TDateTime;
function IncMilliSecond(const AValue: TDateTime;
const ANumberOfMilliSeconds: Int64 = 1): TDateTime;