计算月数(50分)

  • 主题发起人 主题发起人 LJH1978
  • 开始时间 开始时间
L

LJH1978

Unregistered / Unconfirmed
GUEST, unregistred user!
2001。12。31
2002。10。31
为10个月
如何计算得到?
 
function MonthsBetween(const ANow, AThen: TDateTime): Integer;

Description

Call MonthsBetween to obtain the difference, in months, between two TDateTime values. Because months are not all the same length, MonthsBetween returns an approximation based on an assumption of 30.4375 days per month. Fractional months are not counted. Thus, for example, MonthsBetween reports the difference between Feb 1 and Feb 28 as 0. (For that matter, it reports the difference between Feb 1 and Mar 1 as 0.)
 
看不太懂:(
 
我想确切的月数是有问题的。
倒不如先化成天数的差距,再除以30
月数到天数建一个对照表(只十二个月好建) ch(m)
天数到月数的对照表,ch2(x)返回余数。 ch3(x)返回月数
y1,m1,d1 y2,m2,d2
t1=y1*365+ch(m1)+d1
t2=y2*365+ch(m2)+d2
d=ch2((t1-t2) mod 365 )
m=ch3((t1-t2) mod 365 )
月数=(t1-t2)/365 *12+m
 
uses
DateUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
D1, D2: TDateTime;
begin
D1 := StrToDate('2002-01-01');
D2 := StrToDate('2002-05-18');
ShowMessage(IntToStr(MonthsBetween(D1, D2)));

end;
 
天数、月数、年数的都好算,关键看计算规则(客户应该有相应的计算规则)
比如工龄问题,采用的是进位计算,不满一年按一年计算
 
function getmonth(date1,date2:Tdate):integer;
var
year1,year2:integer;
month1,month2:integer;
day1,day2:integer;
begin
DecodeDate(date1, Year1, Month1, Day1);
DecodeDate(date2, Year2, Month2, Day2);
result:=(year2-year1)*12+month2-month1;
end;

ok
 
多人接受答案了。
 
后退
顶部