Ir para conteúdo
Fórum Script Brasil
  • 0

Data


Guest D4rk Schn31d3r

Pergunta

Guest D4rk Schn31d3r

Alguém sabe, como fazer pra subtrair 2 datas e retornar o número de dias entre elas, tipo assim:

ex:

Data 1: 06/03/2004 / Data 2: 09/03/2004

, ..., retornar 3 dias;

No evento onClick de um botão qualquer, coloquei o seguinte código:

procedure Tform1.Button1Click(Sender: TObject);

begin

showmessage(datetostr(strtodate(edtData2.Text) - strtodate(edtData1.Text)));

end;

Com esse código está retornando 02/01/1900!!! Como fazer para retornar a diferença entre as datas, ou seja, 3 dias?

Link para o comentário
Compartilhar em outros sites

4 respostass a esta questão

Posts Recomendados

  • 0

Caro colega, não tenho nada assim no momento, então fiz uma pesquisa na net e trouxe para você uma unit novinha com um exemplo espero que seja util

PS Esta unit foi tirada do programa periodo, um aplicativo exemplo do clube delphi.

abraços

unit UPeriodo;

interface

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  StdCtrls, Buttons;

type

  TForm1 = class(TForm)

    Button1: TButton;

    GroupBox1: TGroupBox;

    Edit1: TEdit;

    Label4: TLabel;

    Edit2: TEdit;

    Label5: TLabel;

    GroupBox2: TGroupBox;

    lblDias: TLabel;

    lblDatas: TLabel;

    lblData1: TLabel;

    lblData2: TLabel;

    BitBtn1: TBitBtn;

    procedure Button1Click(Sender: TObject);

  private

      { Private declarations }

  public

  Procedure Resultado;

    { Public declarations }

  end;

var

                      Form1: TForm1;

          D1,D2,M1,M2,A1,A2: String;

Periodo1,Periodo2,Ano,Valor,Bis1,Bis2: Integer;

implementation

{$R *.DFM}

function AnoBis1(Data: TDateTime): Boolean;

var

Dia,Mes,Ano : Word;

begin

DecodeDate(Data,Ano,Mes,Dia);

if Ano mod 4 <> 0 then

  Bis1 := 0

else

  if Ano mod 100 <> 0 then

      Bis1 := 1

  else

      if Ano mod 400 <> 0 then

        Bis1 := 0

      else

        Bis1 := 1;

end;

function AnoBis2(Data: TDateTime): Boolean;

var

Dia,Mes,Ano : Word;

begin

DecodeDate(Data,Ano,Mes,Dia);

if Ano mod 4 <> 0 then

  Bis2 := 0

else

  if Ano mod 100 <> 0 then

      Bis2 := 1

  else

      if Ano mod 400 <> 0 then

        Bis2 := 0

      else

        Bis2 := 1;

end;

Procedure TForm1.Resultado;

begin

lblDatas.Caption:='Data2 - Data1 =';

lblDias.Caption:='Resultado em dias: '+IntToStr(Ano+Valor)+' dia(s)';

lblData1.Caption:='Data1 Periodo: '+IntToStr(Periodo1);

lblData2.Caption:='Data2 Periodo: '+IntToStr(Periodo2);

end;

Procedure Periodo(P1,P2,D1,D2,A1,A2: Integer);

var A,B,Mes1,Mes2: Integer;

begin

Mes1:=P1;

Mes2:=P2;

Case P1 of

  1 : P1:= 31;

  2 : P1:= 59;

  3 : P1:= 90;

  4 : P1:= 120;

  5 : P1:= 151;

  6 : P1:= 181;

  7 : P1:= 212;

  8 : P1:= 243;

  9 : P1:= 273;

  10 : P1:= 304;

  11 : P1:= 334;

  12 : P1:= 365;

End;

Case P2 of

  1 : P2:= 31;

  2 : P2:= 59;

  3 : P2:= 90;

  4 : P2:= 120;

  5 : P2:= 151;

  6 : P2:= 181;

  7 : P2:= 212;

  8 : P2:= 243;

  9 : P2:= 273;

  10 : P2:= 304;

  11 : P2:= 334;

  12 : P2:= 365;

End;

Case Mes1 of

  1 : Mes1:= 31;

  2 : Mes1:= 28;

  3 : Mes1:= 31;

  4 : Mes1:= 30;

  5 : Mes1:= 31;

  6 : Mes1:= 30;

  7 : Mes1:= 31;

  8 : Mes1:= 31;

  9 : Mes1:= 30;

  10 : Mes1:= 31;

  11 : Mes1:= 30;

  12 : Mes1:= 31;

End;

Case Mes2 of

  1 : Mes2:= 31;

  2 : Mes2:= 28;

  3 : Mes2:= 31;

  4 : Mes2:= 30;

  5 : Mes2:= 31;

  6 : Mes2:= 30;

  7 : Mes2:= 31;

  8 : Mes2:= 31;

  9 : Mes2:= 30;

  10 : Mes2:= 31;

  11 : Mes2:= 30;

  12 : Mes2:= 31;

End;

if Bis1 = 1 Then

A:=(P1-(Mes1-D1))

else

if P1 = 28 then

A:=(P1-(Mes1-D1)+1)

else

A:=(P1-(Mes1-D1));

Periodo1:=A;

if Bis2 = 1 Then

B:=(P2-(Mes2-D2))

else

if P2 = 28 then

B:=(P2-(Mes2-D2)+1)

else

B:=(P2-(Mes2-D2));

Periodo2:=B;

Valor:=(B-A);

Ano:=((A2-A1)*366);

end;

procedure TForm1.Button1Click(Sender: TObject);

Var  Data1, Data2: TDate;

begin

AnoBis1(StrToDate(Edit1.Text));

AnoBis2(StrToDate(Edit2.Text));

Data1:=StrToDate(Edit1.Text);

D1:=FormatDateTime('d',Data1);

M1:=FormatDateTime('m',Data1);

A1:=FormatDateTime('yyyy',Data1);

Data2:=StrToDate(Edit2.Text);

D2:=FormatDateTime('d',Data2);

M2:=FormatDateTime('m',Data2);

A2:=FormatDateTime('yyyy',Data2);

Periodo(StrToInt(M1),StrToInt(M2),StrToInt(D1),StrToInt(D2),StrToInt(A1),StrToInt(A2));

Resultado;

end;

end.

Rafael Marciano

Link para o comentário
Compartilhar em outros sites

  • 0
Caro colega, não tenho nada assim no momento, então fiz uma pesquisa na net e trouxe para você uma unit novinha com um exemplo espero que seja util

PS Esta unit foi tirada do programa periodo, um aplicativo exemplo do clube delphi.

abraços

unit UPeriodo;

interface

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  StdCtrls, Buttons;

type

  TForm1 = class(TForm)

    Button1: TButton;

    GroupBox1: TGroupBox;

    Edit1: TEdit;

    Label4: TLabel;

    Edit2: TEdit;

    Label5: TLabel;

    GroupBox2: TGroupBox;

    lblDias: TLabel;

    lblDatas: TLabel;

    lblData1: TLabel;

    lblData2: TLabel;

    BitBtn1: TBitBtn;

    procedure Button1Click(Sender: TObject);

  private

      { Private declarations }

  public

  Procedure Resultado;

    { Public declarations }

  end;

var

                      Form1: TForm1;

          D1,D2,M1,M2,A1,A2: String;

Periodo1,Periodo2,Ano,Valor,Bis1,Bis2: Integer;

implementation

{$R *.DFM}

function AnoBis1(Data: TDateTime): Boolean;

var

Dia,Mes,Ano : Word;

begin

DecodeDate(Data,Ano,Mes,Dia);

if Ano mod 4 <> 0 then

   Bis1 := 0

else

   if Ano mod 100 <> 0 then

      Bis1 := 1

   else

      if Ano mod 400 <> 0 then

         Bis1 := 0

      else

         Bis1 := 1;

end;

function AnoBis2(Data: TDateTime): Boolean;

var

Dia,Mes,Ano : Word;

begin

DecodeDate(Data,Ano,Mes,Dia);

if Ano mod 4 <> 0 then

   Bis2 := 0

else

   if Ano mod 100 <> 0 then

      Bis2 := 1

   else

      if Ano mod 400 <> 0 then

         Bis2 := 0

      else

         Bis2 := 1;

end;

Procedure TForm1.Resultado;

begin

lblDatas.Caption:='Data2 - Data1 =';

lblDias.Caption:='Resultado em dias: '+IntToStr(Ano+Valor)+' dia(s)';

lblData1.Caption:='Data1 Periodo: '+IntToStr(Periodo1);

lblData2.Caption:='Data2 Periodo: '+IntToStr(Periodo2);

end;

Procedure Periodo(P1,P2,D1,D2,A1,A2: Integer);

var A,B,Mes1,Mes2: Integer;

begin

Mes1:=P1;

Mes2:=P2;

Case P1 of

   1 : P1:= 31;

   2 : P1:= 59;

   3 : P1:= 90;

   4 : P1:= 120;

   5 : P1:= 151;

   6 : P1:= 181;

   7 : P1:= 212;

   8 : P1:= 243;

   9 : P1:= 273;

  10 : P1:= 304;

  11 : P1:= 334;

  12 : P1:= 365;

End;

Case P2 of

   1 : P2:= 31;

   2 : P2:= 59;

   3 : P2:= 90;

   4 : P2:= 120;

   5 : P2:= 151;

   6 : P2:= 181;

   7 : P2:= 212;

   8 : P2:= 243;

   9 : P2:= 273;

  10 : P2:= 304;

  11 : P2:= 334;

  12 : P2:= 365;

End;

Case Mes1 of

   1 : Mes1:= 31;

   2 : Mes1:= 28;

   3 : Mes1:= 31;

   4 : Mes1:= 30;

   5 : Mes1:= 31;

   6 : Mes1:= 30;

   7 : Mes1:= 31;

   8 : Mes1:= 31;

   9 : Mes1:= 30;

  10 : Mes1:= 31;

  11 : Mes1:= 30;

  12 : Mes1:= 31;

End;

Case Mes2 of

   1 : Mes2:= 31;

   2 : Mes2:= 28;

   3 : Mes2:= 31;

   4 : Mes2:= 30;

   5 : Mes2:= 31;

   6 : Mes2:= 30;

   7 : Mes2:= 31;

   8 : Mes2:= 31;

   9 : Mes2:= 30;

  10 : Mes2:= 31;

  11 : Mes2:= 30;

  12 : Mes2:= 31;

End;

if Bis1 = 1 Then

A:=(P1-(Mes1-D1))

else

if P1 = 28 then

A:=(P1-(Mes1-D1)+1)

else

A:=(P1-(Mes1-D1));

Periodo1:=A;

if Bis2 = 1 Then

B:=(P2-(Mes2-D2))

else

if P2 = 28 then

B:=(P2-(Mes2-D2)+1)

else

B:=(P2-(Mes2-D2));

Periodo2:=B;

Valor:=(B-A);

Ano:=((A2-A1)*366);

end;

procedure TForm1.Button1Click(Sender: TObject);

Var  Data1, Data2: TDate;

begin

AnoBis1(StrToDate(Edit1.Text));

AnoBis2(StrToDate(Edit2.Text));

Data1:=StrToDate(Edit1.Text);

D1:=FormatDateTime('d',Data1);

M1:=FormatDateTime('m',Data1);

A1:=FormatDateTime('yyyy',Data1);

Data2:=StrToDate(Edit2.Text);

D2:=FormatDateTime('d',Data2);

M2:=FormatDateTime('m',Data2);

A2:=FormatDateTime('yyyy',Data2);

Periodo(StrToInt(M1),StrToInt(M2),StrToInt(D1),StrToInt(D2),StrToInt(A1),StrToInt(A2));

Resultado;

end;

end.

Rafael Marciano

Pra que complicar....

v se resolve.

function DifDias(DataVenc:TDateTime; DataAtual:TDateTime): String;

Var Data: TDateTime;

dia, mes, ano: Word;

begin

if DataAtual < DataVenc then Result := 'Null'

else

begin

Data := DataAtual - DataVenc;

DecodeDate( Data, ano, mes, dia);

Result := FloatToStr(Data);

end;

end;

cool.gif Fui.

Link para o comentário
Compartilhar em outros sites

  • 0

Descobri uma maneira mais fácil de resolver isso:

é só trocar DateToStr por FloatToStr e resolvido o problema, assim:

procedure Tform1.Button1Click(Sender: TObject);

begin

showmessage(floattostr(strtodate(edtData2.Text) - strtodate(edtData1.Text)));

end;

, de qualquer forma, obrigado pela ajuda.

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novos posts.


  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      651,9k
×
×
  • Criar Novo...