날짜 계산함수입니다....
function DateAdd(interval:TDateAdd_interval; Number:integer; DateTime:TDateTime):TDateTime;
var ts:TTimeStamp;
st:TSystemTime;
MDays:integer;
AllMonth:integer;
procedure TimeAdd(Ds,Mt:integer);
begin
ts.Date := ts.Date + Number div Ds;
ts.Time := (Number mod Ds) * Mt;
end;
begin
result:=DateTime;
if number=0 then exit;
if interval in [di_Msec,di_Sec,di_Minute,di_hour,di_Day] then begin
ts:=DateTimeToTimeStamp(DateTime);
case interval of
di_Msec: TimeAdd(86400000,1);
di_Sec: TimeAdd(86400,1000);
di_Minute:TimeAdd(1440,60000);
di_hour: TimeAdd(24,3600000);
di_Day: ts.Date := ts.Date + number;
end;
while ts.Time<0 do begin
ts.Date:=ts.Date -1;
ts.Time:=ts.Time + 86400000;
end;
result := TimeStampToDateTime(ts);
end else begin
DateTimeToSystemTime(DateTime,st);
case interval of
di_Month: begin
AllMonth:=st.wYear * 12 + st.wMonth + number;
if AllMonth<1 then AllMonth:=1;
st.wYear:=(AllMonth - 1) div 12;
st.wMonth:=(AllMonth - 1) mod 12 + 1;
end;
di_Year: st.wYear := st.wYear + number;
end;
MDays:=MonthDays[IsLeapYear(st.wYear)][st.wMonth];
if st.wDay>MDays then st.wDay := MDays;
result := SystemTimeToDateTime(st);
end;
end;
날짜 계산함수입니다....
function DateAdd(interval:TDateAdd_interval; Number:integer; DateTime:TDateTime):TDateTime;
var ts:TTimeStamp;
st:TSystemTime;
MDays:integer;
AllMonth:integer;
procedure TimeAdd(Ds,Mt:integer);
begin
ts.Date := ts.Date + Number div Ds;
ts.Time := (Number mod Ds) * Mt;
end;
begin
result:=DateTime;
if number=0 then exit;
if interval in [di_Msec,di_Sec,di_Minute,di_hour,di_Day] then begin
ts:=DateTimeToTimeStamp(DateTime);
case interval of
di_Msec: TimeAdd(86400000,1);
di_Sec: TimeAdd(86400,1000);
di_Minute:TimeAdd(1440,60000);
di_hour: TimeAdd(24,3600000);
di_Day: ts.Date := ts.Date + number;
end;
while ts.Time<0 do begin
ts.Date:=ts.Date -1;
ts.Time:=ts.Time + 86400000;
end;
result := TimeStampToDateTime(ts);
end else begin
DateTimeToSystemTime(DateTime,st);
case interval of
di_Month: begin
AllMonth:=st.wYear * 12 + st.wMonth + number;
if AllMonth<1 then AllMonth:=1;
st.wYear:=(AllMonth - 1) div 12;
st.wMonth:=(AllMonth - 1) mod 12 + 1;
end;
di_Year: st.wYear := st.wYear + number;
end;
MDays:=MonthDays[IsLeapYear(st.wYear)][st.wMonth];
if st.wDay>MDays then st.wDay := MDays;
result := SystemTimeToDateTime(st);
end;
end;
사용방법은 다음과 같습니다..
dt := now //오늘 날짜와시간....
DateAdd(di_Sec, 10, dt) //현재시간에서 10초후
DateAdd(di_Minute, 10, dt) //현재시간에서 10분후
DateAdd(di_Hour, 10, dt) //현재시간에서 10시간후
DateAdd(di_Day, 10, dt) //오늘 날짜에서 10일후
DateAdd(di_Month, 10, dt) //현재월 에서 10개월후
DateAdd(di_Year, 10, dt) //현재년에서 10년후
마이너스(-) 값을 입력하시면 꺼꾸로 계산되겠죠...
그러면 원하시는 결과를 얻 을 수 있을겁니다...^^...