给你一个思路,你自己试试看吧.
取出两个日期间所有的月份,如: 2001.1--2002.2 有,1,2,3,4,5,6,7,8,9,10,11,12,1,2
将他门动态放入一数组(ArrMon)中.对数组进行循环.用两个记数变量(iSbn,iXbn)记录.
iSbn 就是上半年的总数, iXbn 就是下半年的总数.
如:
iSbn:=0;
iXbn:=0;
for iLoop:= Low(ArrMon) to High(ArrMon) do begin
if (ArrMon[iLoop] >= 1) and (ArrMon[iLoop]<=6) then
Inc(iSbn)
else if (ArrMon[iLoop] >= 7) and (ArrMon[iLoop]<=12) then
Inc(iXbn) ;
end;
var i,lsbn,lxbn: Integer;
lsbn :=(Lastyear-Firstyear)*6;
lxbn :=(Lastyear-Firstyear)*6;
for i :=FirstMonth to 12 do
if i<= 6 then
lsbn :=lsbn+1
else
lxbn :=lsbn+1;
for i :=1 to LastMonth do
if i<= 6 then
lsbn :=lsbn+1
else
lxbn :=lsbn+1;
var
{month1,month2分别为年份1,年份的月份,monthcount1,monthcount2为上、下半年的月份总数}
month1,month2,monthcount1,monthcount2:smallint;
if (month1<=6) then
begin
monthcount1:=6-month1+1;
monthcount2:=6;
end
else
begin
monthcount1:=0;
monthcount2:=month2-6;
end;
if (month2<=6) then monthcount1:=mounthcount1+(6-month2+1)
else
begin
monthcount1:=monthcount1+6;
monthcount2:=monthcount2+(month2-6)
end;
function GetSMonthCount(AStartDate, AEndDate: TDate; var AUpMonthCount, ADownMonthCount: integer): boolean;
var
y1, y2, m1, m2, d1, d2: word;
begin
AUpMonthCount:=0;
ADownMonthCount:=0;
if AEndDate<AStartDate then begin
result:=false;
exit;
end;
decodedate(AStartDate, y1, m1, d1);
decodedate(AEndDate, y2, m2, d2);
if y1=y2 then begin
if (m1<=6) and (m2<=6) then begin
AUpMonthCount:=m2-m1+1;
end else if (m1<=6) and (m2>6) then begin
AUpMonthCount:=7-m1;
ADownMonthCount:=m2-6;
end else if (m1>6) and (m2>6) then begin
ADownMonthCount:=m2-m1+1;
end;
end else begin
if m1<=6 then begin
AUpMonthCount:=7-m1;
ADownMonthCount:=6;
end else begin
ADownMonthCount:=m1-6;
end;
if m2<=6 then begin
AUpMonthCount:=AUpMonthCount+m2;
end else begin
AUpMonthCount:=AUpMonthCount+6;
ADownMonthCount:=ADownMonthCount+(m2-6);
end;
if (y2-y1)>1 then begin
AUpMonthCount:=AUpMonthCount+6*(y2-y1);
ADownMonthCount:=ADownMonthCount+6*(y2-y1);
end;
end;
result:=true;
end;