用incmonth函数累加月数出现如下问题(简写):
a=incmonth(2003-01-31,1);a=2003-02-28
b=incmonth(a,1);b=2003-03-28
于是月底就成了28号
在做MIS程序计算天数时就会出错。有什么办法可以解决?

解决方案 »

  1.   

    手工合成日期,使用EncodeDate函数!!!
      

  2.   

    注意!IncMonth函数参数必须是TDateTime类型可以这样:
    var t:Tdatetime;
    ...
    t:=IncMonth(StrToDatetime(2003-01-31),1);b:=DateTimeToStr(a);
    b 就是2月28日了
      

  3.   

    UnitDateUtilsCategorydatetime routinesDelphi syntax:function DaysInAMonth(const AYear, AMonth: Word): Word;C++ syntax:extern PACKAGE Word __fastcall DaysInAMonth(const Word AYear, const Word AMonth);DescriptionCall DaysInAMonth to obtain the number of days in the specified month of the specified year.AYear is a year between 1 and 9999 (inclusive).AMonth is a month between 1 and 12 (inclusive).
      

  4.   

    得到月底日期:
     1)取本月的第一天  
        s:=formatDateTime('yyyy-mm-01',testDate);
        firstDay:=strToDateTime(s);
     2) 本月的第一天 加 32 (因为每个月不可能超过32天)
        nextMonth := firstDay + 32;
     3) 取下个月的第一天    
        s:=formatDateTime('yyyy-mm-01',nextMonth);
        firstDay:=strToDateTime(s);
     4) 下个月第一天 减 一 ,即的本月底日期
        resultDay:=firstDay - 1;
        resultStr:=formatDateTime('yyyy-mm-dd',resultDay);
      

  5.   

    var Date:Tdate;
    Date:=IncMonth(StrToDatetime(2003-01-31),1);b:=DateTimeToStr(Date);
    b 就是2月28日了