MonthsBetween(const ANow, AThen: TDateTime): Integer;
YearsBetween(const ANow, AThen: TDateTime): Integer;

解决方案 »

  1.   

    UnitDateUtils
    function MonthOf(const AValue: TDateTime): Word;//有它了,还算不出来
    function YearOf(const AValue: TDateTime): Word;//也有一样;
    function dayOf(const AValue: TDateTime): Word;
      

  2.   

    年份差 = Select Year(Date1)-Year(Date2)
    月份差 = Select Month(Date1)-Month(Date2)
      

  3.   

    DecodeDate(Date1,year1,month1,day1);
    DecodeDate(Date2,year2,month2,day2);
    year1-year2=年份差;
    month1-month2=月份差;
    date1-date2=天数差
      

  4.   

    我是delphi5好像没有
    MonthsBetween
    和Monthof等函数阿
      

  5.   

    在DateUtils单元中
    D6;不知道D5有没有这个单元 如果没有,
    只能这样算了
    Yearof(Date1)-Yearof(Date2)
    Monthof(Date1)-Monthof(Date2)
      

  6.   

    对不起,yearof和monthof也是dateutils单元中的,
    那你想别的办法吧。
      

  7.   

    decodedate(date1,year1,month1,day1);
    decodedate(date2,year2,month2,day2);
    以后的知道该怎么做了吧
      

  8.   

    var
      wYear1,wYear2,wMonth1,wMonth2,wDay1,wDay2:word;
      wYearDiff,wMonthDiff
    begin
      DecodeDate(date1,wYear1,wMonth1,wDay1);
      DecodeDate(date2,wYear2,wMonth2,wDay2);
      wYearDiff:=wYear2-wYear1;
      wMonthDiff:=wMonth2-wMonth1;
    end;
      

  9.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      dt1,dt2: TDateTime;
      md,yd: Integer;  //md月份差,yd年份差
    begin
      md := 0;
      dt1 := StrToDateTime('2002-05-31');
      dt2 := StrToDateTime('2000-03-01');
      while FormatDateTime('yyyy-mm',dt1)>FormatDateTime('yyyy-mm',dt2) do
      begin
        Inc(md);
        dt2 := IncMonth(dt2,1);
      end;
      yd := md div 12;
      ShowMessage(IntToStr(md));
      ShowMessage(IntToStr(yd));
    end;
      

  10.   

    两个日期相减,得到的小数点前的单位为天,小数点后的为小时
    你把天转化为年应该就可以了
    不过还得考虑闰年了!
    如果你是用SQL server数据库的话,建议你直接只用SQL 的函数
    在select中把它计算出来更方便!