我两个日期型变量A='2001-1-1',B='2002-2-22';
如何判断 A-B>1年(即A与B是否相差一年)

解决方案 »

  1.   

    Var
      DataSp:Integer;
      A,B:string;
    ...
      DataSP:=strtodate(A)-strtodate(B);
      if DataSP>365+366 then 
        showmessage('超过两年');
      

  2.   

    var
      Date1, Date2: TDate;
      Y1, M1, D1, Y2, M2, D2, Y, M, D: Word;
    begin
      Date1 := StrToDate('2001-1-1');
      Date2 := StrToDate('2002-2-22');
      DecodeDate(Date1, Y1, M1, D1);
      DecodeDate(Date2, Y2, M2, D2);
      Y := Y2 - Y1;
      M := M2 - M1;
      D := D2 - D1;
      if (Y > 1)
        or ((Y = 1) and (M > 0))
        or (((Y = 1) and (M = 0)) and (D > 0)) then
          ShowMessage('Date2>Date1');
    end;————————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    ————————————————————————————————————
      

  3.   

    var n:integer;
    begin
    n:=yearsbetween(DateTimePicker1.DateTime,DateTimePicker2.DateTime);
     edit1.Text:=inttostr(n);
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      a,b:string;
    begin
      a:=FormatDateTime('yyyymmdd',DateTimePicker1.Date);
      b:=FormatDateTime('yyyymmdd',DateTimePicker2.Date);
      if copy(a,1,4)=copy(b,1,4) then
      begin
        showmessage('一样的!:)');
      end else showmessage('不一样的!:(');
    end;
      

  5.   

    Var
      DataSp:Integer;
      A,B:string;
    ...
      DataSP:=strtodate(A)-strtodate(B);
      if DataSP>365+366 then 
        showmessage('超过两年');
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    a,b:Tdate;
    begin
    A:=strtodate('2001-1-1');
    B:=strtodate('2002-1-1');
     if (b-a)>=365 then showmessage('大于一年');end;