根据给出的两个日期算月份数...(120分)

L

ludao

Unregistered / Unconfirmed
GUEST, unregistred user!
给定两个日期,怎样算出属于上半年的月份有多少个,属于下半年的月份有多少个.
比如:2001.1--------2002.2,则上半年的有8个月,下半年的有6个月.
给出代码,谢谢!
 
给你一个思路,你自己试试看吧.
取出两个日期间所有的月份,如: 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;

 
DEN,麻烦你把动态数组的定义和如何赋值也写出来吧!呵呵!然后120分就是你的拉,呵呵!谢谢!
 
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;
 
DEN的代码最简单,可惜动态数组的赋值没对俺说:(
我是用njhmq的方法解决的,虽然提供的代码有点小错误,呵呵!
WilliamGui兄的代码太复杂了:(
 
顶部