已知出生年月日,求当前出生月数,如何编写代码,求
谢谢

解决方案 »

  1.   

    如果,你的出生年月日是TDateTime型,可用MonthOf
    uses DateUtils;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      t:tdatetime;
    begin
      t:=strtodate('1999-09-10');
      ShowMessage(inttostr(monthof(t)));
    end;
      

  2.   

    如果你的要求不是很精确(设一月=30.4375天),可用:
    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;
      

  3.   

    另一方法:
    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;
      

  4.   

    m := (y1-y2) * 12 + (m1 - m2) ;
    改成
    m := (y1-y2) * 12 + (m1 - m2) + (  Trunc( (d2 -d2) / 30 - 0.99 )   );
    可能更加完善点,我想,因为比如出生是8号,但现在才5号,所以月数害的减去一个月,不知道对不对,^_^
      

  5.   

    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.)
      

  6.   

    DateUtils单元中有MonthsBetween这个函数,不过它的计算不是很精确的,它以一个月平均30.4375天的时间来计算.所以2月1号到2月28号并不算是1个月而是0个月