日期相减,得到相差的月份,有没有简单的方法啊?

解决方案 »

  1.   

    看你的实际需要了,
    monthsbetween 认为一个月有30.4375天,两个datetime之间要是差不到这些天,它返回的就是0,比如2003-2-1和2003-3-1返回就是0
    如果你程序中认为即使是2003-2-28和2003-3-1也要返回1的话,那就用monthof,把两个都monthof一下,再减就ok了。
      

  2.   

    有这样的函数的:Returns the approximate number of months between two specified TDateTime values.UnitDateUtilsCategorydate/time routinesfunction 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.)
      

  3.   

    比较一下
    procedure TForm1.Button1Click(Sender: TObject);
    var
      imonth1, imonth2: integer;
    begin
      imonth1 := MonthOf(strtodate('2003-3-1'));
      imonth2 := MonthOf(strtodate('2003-2-1'));
      ShowMessage(inttostr(imonth1 - imonth2));
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      date1, date2: TDate;
    begin
      date1 := strtodate('2003-3-1');
      date2 := strtodate('2003-2-1');
      ShowMessage(inttostr(MonthsBetween(date1, date2)));
    end;