function MonthsBetween(const ANow, AThen: TDateTime): Integer;DescriptionCall 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.)
uses DateUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
t:tdatetime;
begin
t:=strtodate('1999-09-10');
ShowMessage(inttostr(monthof(t)));
end;
uses DateUtils,Math;
procedure TForm1.Button1Click(Sender: TObject);
var
t:tdatetime;
d:double;
m:integer;
begin
t:=strtodate('2004-8-8');
d:=MonthSpan(now(),t);
m:=Round(d);
ShowMessage(inttostr(m));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
vDate : TDateTime;
y1,m1,d1,y2,m2,d2 : Word;
m : Integer;
Begin
DecodeDate(now(),y1,m1,d1);
DecodeDate(strtodate('2004-8-8'),y2,m2,d2);
m := (y1-y2) * 12 + (m1 - m2) ;
ShowMessage(inttostr(m));
End;
改成
m := (y1-y2) * 12 + (m1 - m2) + ( Trunc( (d2 -d2) / 30 - 0.99 ) );
可能更加完善点,我想,因为比如出生是8号,但现在才5号,所以月数害的减去一个月,不知道对不对,^_^