根据dingbaosheng算法,DELPHI//public function define
//----------------------------
function IsPinYear(year):boolean;
//判断闰年
begin
if ((year mod 4=0) and ((year mod 100<>0) or (year mod 400= 0))) then
Result := True
else
Result := False;
end;
function getdays(year,month):integer;//根据年月取天数
begin
if month in[1,3,5,7,8,10,12] then
Result := 31
else
Result :=30;
//处理2月得天数
if (month=2) then
if (IsPinYear(year)) then
Result := 29
else
Result := 28;
end;
// 获得自从1970-01-01到目标日期得总天数。
function GetNumDate( const wYear,wMonth,wDay:word):integer;
var
iY,iM:integer;
begin
Result :=0;
//计算年得天数
for iY := 1971 to wYeaydo
for iM := 1 to 12do
Result := Result + getdays(iY,iM);
//计算月得天数
for iM := 2 to wMonthdo
Result := Result + getdays(wYear,iM);
//得天数
Result := Result + wDay-1;
end;
//获取年,月,日
procedure DecodeStrDate(const strDate:string;var wYeay,wMonth,wDay:word)
begin
wYear := strtoint(copy(strDate,1,4));
wMon := strtoint(copy(strDate,5,2));
wDay := strtoint(copy(strDate,7,2));
end;
//获取天数,日期格式为YYYYMMDD,如2006-10-13为20061013。
function GetCompareDate(const FromDate,toDate:string):integer;
var
fYear,fMon,fDay,tYear,tMon,tDay:word;
begin
DecodeStrDate(FromDate,fYeay,fMonth,fDay);
DecodeStrDate(ToDate,tYeay,tMonth,tDay);
//2个自1970-01-01日起得总天数相减,得出这2个日期之间得天数。
Result := GetNumDate( tYear,tMonth,tDay)-GetNumDate( fYear,fMonth,fDay);
end;