uses DateUtils;(*
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;
*)
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;
*)
function IncMonth(const Date: TDateTime; NumberOfMonths: Integer): TDateTime;
DecodeData(aDate, Y, M, D);
M:=M+dM;
Y:=Y+ (M+11) div 12 - 1;
M:=(M+11) mod 12 -1;
Result := EncodeDate(Y,M,D);
begin
ShowMessage(DateToStr(IncMonth(StrToDate('2002-01-20'), 6)));
ShowMessage(DateToStr(IncMonth(StrToDate('2002-01-20'), 18)));
ShowMessage(DateToStr(IncMonth(StrToDate('2002-01-20'), -6))); //不要忘记还可以这样
end;
用DecodeDate 把年份和月份分离出来,把月分相加,然后再用 mod函数 求得
月份 mod 12 余数就是所求月份,用 div 求得除数,这里要使用到trunc函数.
除数就是要加的年份,然后用encodedatE就可以全成新的日期了.
你用sql语句来加月份,这样就不用你考虑什么二月怎么办,30天的月和31天的月怎么办,闰年怎么办之类的问题,举个例子:...
query_test.close;
query_test.sql.clear;
query_test.sql.add('values(date('2002-4-3')+6 month)');
query_test.open;
showmessage(query_test.fields[0].asstring);
query_test.close;
...
好久不来csdn了,现在怎么多了一些五角星和三角?是什么东东?还有,上面的query_test.sql.add('values(date('2002-4-3')+6 month)');
应该写成query_test.sql.add('values(date(''2002-4-3'')+6 month)');
刚才没注意,给写错了
嘻嘻嘻嘻好久不见嘻嘻嘻嘻嘻
嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻
日期其实是一个double类型,整数部分代表日期,小数部分代表时间,
想加一个月,就加30
管他一个月几天?!