DateUtils单元的MonthsBetween的Function:求月份的差数
showmessage(inttostr(MonthsBetween(StrToDate('2004-10-31'),StrToDate('2004-11-30'))));
的结果是0!这难道是Monthbetween的BUG?

解决方案 »

  1.   

    是你以為錯了, 看一下源碼:function MonthsBetween(const ANow, AThen: TDateTime): Integer;
    begin
      Result := Trunc(MonthSpan(ANow, AThen));
    end;
    function MonthSpan(const ANow, AThen: TDateTime): Double;
    begin
      Result := DaySpan(ANow, AThen) / ApproxDaysPerMonth;
    end;
    var
      ApproxDaysPerMonth: Double = 30.4375;function DaySpan(const ANow, AThen: TDateTime): Double;
    begin
      Result := SpanOfNowAndThen(ANow, AThen);
    end;只是說這兩個日期, 大概相差多少個月
      

  2.   

    Returns the approximate number of months between two specified TDateTime values.
    幫助中也說,
    approximate 大概的意思!!如果你想得到你原來的意思, 應該用
    MonthOf(XXX) - MonthOf(YYY)