delphi 中对 ‘年-月’进行加减操作,如对字符串型“2007-07”减一个月,有什么函数可以使用。

解决方案 »

  1.   

    uses DateUtils;IncMonth(日期, -1);
      

  2.   

    看date/time routines里的函数,这里有操作date/time的所有的函数.
      

  3.   

    function IncMonthStr(Dt: string; val: integer): string;
    begin
      Result := FormatDatetime('YYYY-MM', IncMonth(StrtoDate(Dt+'-01'), val));
    end;procedure TForm1.Button3Click(Sender: TObject); {测试}
    var
      S1, S2: string;
    begin
      S1 := '2007-01';
      S2 := IncMonthStr(S1, -1);
      Showmessage(S2);
    end;
      

  4.   

    转日期然后用
    IncMonth()加减
      

  5.   

    {多种测试}{但并不是通用函数,因为还没有考虑系统日期设置的分隔符问题,自己控制使用而已}function IncMonthStr(Dt: string; val: integer): string;
    begin
      Result := FormatDatetime('YYYY-MM', IncMonth(StrtoDate(Dt+'-01'), val));
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      S1, S2: string;
    begin
      S1 := '2007-01';
      S2 := IncMonthStr(S1, -1); //减1个月
      Showmessage(S2);  S2 := IncMonthStr(S1, 2); //加2个月
      Showmessage(S2);  S2 := IncMonthStr(S1, 13); //加13个月
      Showmessage(S2);  S2 := IncMonthStr(S1, -6); //减6个月
      Showmessage(S2);
    end;