procedure TForm1.Button2Click(Sender: TObject);
var
  date1,date2,date3,date4:String;
  i,j:Integer;
begin
    date1:='2009-12-01';
    date2:='2010-02-01';
    date3:='2010-01-01';
    date4:='2010-03-01';
    i:=MonthsBetween(StrToDateTime(date1),StrToDateTime(date2));
    j:=MonthsBetween(StrToDateTime(date3),StrToDateTime(date4));
    ShowMessage(IntToStr(i));
    ShowMessage(IntToStr(j));
end;
为什么显示的i是2,而j是1呢,按道理的的话不是都应该是2啊。

解决方案 »

  1.   

    看帮助,有说明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 Mar 1 as 0.
      

  2.   

    这个是delphi的bug,他的时间是按照一个月30.4375几来除的,就是先取到daysBetween,即相隔多少天再除这个值,所以不准的,YearsBetween也不准,是除365.25几按一年来除。这2个有bug,需要算具体的你还是自己写吧
      

  3.   

    不如直接用DateTime运算吧
    DateTime就是一个Double值