做的是一个日期计算的,比如2008年9月8日和2009年4月4日相差多少年多少月多少日。
就有一个"计算日期"按钮。
但是小弟编的代码错误百出,请高手指正,很感谢
下面是代码:
unit work1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Button1: TButton;
    Edit7: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementationvar
  date1:string;
  date2:string;
  temp1:string;
  temp2:string;
  temp3:string;
  day1:integer;
  day2:integer;
  y1:integer;
  y2:integer;
  m1:integer;
  m2:integer;
  d1:integer;
  d2:integer;
  n:integer;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  date1:=edit1.text+edit2.text+edit3.text;
  date2:=edit4.text+edit5.text+edit6.text;
  y1:=strtoint(edit1.text);y2:=strtoint(edit4.text);
  m1:=strtoint(edit2.text);m2:=strtoint(edit5.text);
  d1:=strtoint(edit3.text);d2:=strtoint(edit6.text);
  day1:=strtoint(date1);day2:=strtoint(date2);
  if (y1 mod 400 =0) or ((y1 mod 100<>0) and (y1 mod 4=0))  then
      begin
      n:=1;
      end
  else
      n:=2;
  end ;
  if day1 <= day2 then
      begin
       if(m1<=m2) then
         begin
           if(d1<=d2) then
              begin
               temp1:=inttostr(y2-y1);
               temp2:=inttostr(m2-m1);
               temp3:=inttostr(d2-d1);
              end
           else
                temp1:=inttostr(y2-y1);
                temp2:=inttostr(m2-m1-1);
                case m1 of
                1: temp3:=inttostr(d2-d1+31);
                2: if n=1 then
                   begin
                     temp3:=inttostr(d2-d1+29);
                     end
                   else
                     temp3:=inttostr(d2-d1+28);
                     end;
                3: temp3:=inttostr(d2-d1+31);
                4: temp3:=inttostr(d2-d1+30);
                5: temp3:=inttostr(d2-d1+31);
                6: temp3:=inttostr(d2-d1+30);
                7: temp3:=inttostr(d2-d1+31);
                8: temp3:=inttostr(d2-d1+31);
                9: temp3:=inttostr(d2-d1+30);
                10: temp3:=inttostr(d2-d1+31);
                11: temp3:=inttostr(d2-d1+30);
                12: temp3:=inttostr(d2-d1+31);
           end;
        end
        else
          if(d1<=d2) then
            begin
              temp1:=inttostr(y2-y1-1);
              temp2:=inttostr(m2-m1+12);
              temp3:=inttostr(d2-d1);
            end
          else
              temp1:=inttostr(y2-y1-1);
              temp2:=inttostr(m2-m1+11);
              case m1 of
              1: temp3:=inttostr(d2-d1+31);
              2: if n=1 then
                   begin
                     temp3:=inttostr(d2-d1+29);
                     end
                   else
                     temp3=inttostr(d2-d1+28);
                     end;
              3: temp3:=inttostr(d2-d1+31);
              4: temp3:=inttostr(d2-d1+30);
              5: temp3:=inttostr(d2-d1+31);
              6: temp3:=inttostr(d2-d1+30);
              7: temp3:=inttostr(d2-d1+31);
              8: temp3:=inttostr(d2-d1+31);
              9: temp3:=inttostr(d2-d1+30);
              10: temp3:=inttostr(d2-d1+31);
              11: temp3:=inttostr(d2-d1+30);
              12: temp3:=inttostr(d2-d1+31);
          end;
       end;
  end
  else
     application.messagebox('前后日期次序颠倒','错误');
     exit;
  end;
  edit7.text:=temp1+'年'+temp2+'月'+temp3+'日'end;end.提示的错误有:
[Error] work1.pas(73): Declaration expected but 'IF' found
[Error] work1.pas(95): Undeclared identifier: '3'
[Error] work1.pas(96): Undeclared identifier: '4'
[Error] work1.pas(97): Undeclared identifier: '5'
[Error] work1.pas(98): Undeclared identifier: '6'
[Error] work1.pas(99): Undeclared identifier: '7'
[Error] work1.pas(100): Undeclared identifier: '8'
[Error] work1.pas(101): Undeclared identifier: '9'
[Error] work1.pas(102): Undeclared identifier: '10'
[Error] work1.pas(103): Undeclared identifier: '11'
[Error] work1.pas(104): Undeclared identifier: '12'
[Error] work1.pas(107): '.' expected but 'ELSE' found
[Error] work1.pas(109): Identifier redeclared: 'Finalization'
[Warning] work1.pas(114): Text after final 'END.' - ignored by compiler
[Fatal Error] pro1.dpr(5): Could not compile used unit 'work1.pas'

解决方案 »

  1.   

    把case 
    2: if n=1 then
      begin
      temp3:=inttostr(d2-d1+29);
      end
      else
      temp3:=inttostr(d2-d1+28);
      end;
    里面的代码用begin end括起来
      

  2.   

    delphi在DateUtils单元中有个现成的函数
    DaysBetween(const ANow, AThen: TDateTime): Integer;
      

  3.   

      if (y1 mod 400 =0) or ((y1 mod 100<>0) and (y1 mod 4=0)) then
      begin
      n:=1;
      end
      else
      n:=2;
      end ;
    如果没看错,这里应该多了
      

  4.   

    DaysBetween(const ANow, AThen: TDateTime): Integer;
    没必要自己写的麻烦。
      

  5.   

    所有有else都多了end;改好至少可以编译的
    begin date1:=edit1.text+edit2.text+edit3.text;
      date2:=edit4.text+edit5.text+edit6.text;
      y1:=strtoint(edit1.text);y2:=strtoint(edit4.text);
      m1:=strtoint(edit2.text);m2:=strtoint(edit5.text);
      d1:=strtoint(edit3.text);d2:=strtoint(edit6.text);
      day1:=strtoint(date1);day2:=strtoint(date2);
      if (y1 mod 400 =0) or ((y1 mod 100<>0) and (y1 mod 4=0)) then
      begin
      n:=1;
      end
      else
      n:=2;
     // end ; 这里多end;
      if day1 <= day2 then
      begin
      if(m1<=m2) then
      begin
      if(d1<=d2) then
      begin
      temp1:=inttostr(y2-y1);
      temp2:=inttostr(m2-m1);
      temp3:=inttostr(d2-d1);
      end
      else
      temp1:=inttostr(y2-y1);
      temp2:=inttostr(m2-m1-1);
      case m1 of
      1: temp3:=inttostr(d2-d1+31);
      2: if n=1 then
      begin
      temp3:=inttostr(d2-d1+29);
      end
      else
      temp3:=inttostr(d2-d1+28);
     // end; 这里多end;
      3: temp3:=inttostr(d2-d1+31);
      4: temp3:=inttostr(d2-d1+30);
      5: temp3:=inttostr(d2-d1+31);
      6: temp3:=inttostr(d2-d1+30);
      7: temp3:=inttostr(d2-d1+31);
      8: temp3:=inttostr(d2-d1+31);
      9: temp3:=inttostr(d2-d1+30);
      10: temp3:=inttostr(d2-d1+31);
      11: temp3:=inttostr(d2-d1+30);
      12: temp3:=inttostr(d2-d1+31);
      end;
      end
      else
      if(d1<=d2) then
      begin
      temp1:=inttostr(y2-y1-1);
      temp2:=inttostr(m2-m1+12);
      temp3:=inttostr(d2-d1);
      end
      else
      temp1:=inttostr(y2-y1-1);
      temp2:=inttostr(m2-m1+11);
      case m1 of
      1: temp3:=inttostr(d2-d1+31);
      2: if n=1 then
      begin
      temp3:=inttostr(d2-d1+29);
      end
      else
      temp3:=inttostr(d2-d1+28); ///这个=应该是:=
      //end; 这里多end;
      3: temp3:=inttostr(d2-d1+31);
      4: temp3:=inttostr(d2-d1+30);
      5: temp3:=inttostr(d2-d1+31);
      6: temp3:=inttostr(d2-d1+30);
      7: temp3:=inttostr(d2-d1+31);
      8: temp3:=inttostr(d2-d1+31);
      9: temp3:=inttostr(d2-d1+30);
      10: temp3:=inttostr(d2-d1+31);
      11: temp3:=inttostr(d2-d1+30);
      12: temp3:=inttostr(d2-d1+31);
    //  end; 这里多end;
      end;
      end
      else
      application.messagebox('前后日期次序颠倒','错误');
      exit;
     // end; 这里多end;
      edit7.text:=temp1+'年'+temp2+'月'+temp3+'日'end;你的else没有begin,就不要end;