Diferença entre horas
Estou fazendo somatorias com horas extras e cheguei a 2 strings no valor por exemplo de 35:34 e 2:57, se eu tentar usar por exemplo LSaldo.Caption := TimeToStr(s-p), retorna erro por causa do 35, deve ter passado de 24 (horas), alguem tem alguma sujestao para eu conseguir fazer essa conta e saber o saldo de horas ?
Abs e obrigado
Felipe Ip
Curtidas 0
Respostas
Pietro Braga
10/11/2010
Se não me engano o tipo TTime é de ponto flutuante, talvez funcione desse jeito:
LSaldo.Caption := FloatToStr(s-p);
GOSTEI 0
Felipe Ip
10/11/2010
obrigado pela ajuda, nao deu erro, mas tb nao calculou a diferença.
Abs
GOSTEI 0
Felipe Ip
10/11/2010
Alguem tem mais algum sujestao de como calcular ?
Obrigado
GOSTEI 0
Juan Garcia
10/11/2010
Creio que vc precisa converter para as horas para decimal, efetuar os cálculos e depois converter para string.
Segue um exemplo:
unit Main;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TMainForm = class(TForm) SubtraiButton: TButton; Edit1: TEdit; Edit2: TEdit; ResultadoEdit: TEdit; Label1: TLabel; Label2: TLabel; procedure FormCreate(Sender: TObject); procedure SubtraiButtonClick(Sender: TObject); private { Private declarations } public function HorasDecimaisToStr(Qtd: Extended): string; function StrToHorasDecimais(const QtdHoras: String): Extended; { Public declarations } end;var MainForm: TMainForm;implementation{$R *.dfm}uses Math;procedure TMainForm.FormCreate(Sender: TObject);begin Edit1.Text := '35:34'; Edit2.Text := '2:57'; ResultadoEdit.Text := 'nc';end;function TMainForm.HorasDecimaisToStr(Qtd: Extended): string;var h, m: integer;begin h := Trunc(Qtd); m := Trunc((Qtd - h) * 61); // 61 para corrigir erro de arredondamento Result := Format('%d:%2.2d',[h,m]);end;function TMainForm.StrToHorasDecimais(const QtdHoras: String): Extended;var h, m: integer;begin h := StrToInt(Copy(QtdHoras,1,Pos(':',QtdHoras)-1)); m := StrToInt(Copy(QtdHoras,Pos(':',QtdHoras)+1, Length(QtdHoras))); Result := h + m/60;end;procedure TMainForm.SubtraiButtonClick(Sender: TObject);var T1, T2, Resultado: Extended;begin T1 := StrToHorasDecimais(Edit1.Text); Label1.Caption := FloatToStr(T1); T2 := StrToHorasDecimais(Edit2.Text); Label2.Caption := FloatToStr(T2); Resultado := T1 - T2; ResultadoEdit.Text := HorasDecimaisToStr(Resultado);end;end.
Há um erro de arredondamento, por isso dividi por 61 em vez de por 60. Talvez haja uma solução melhor.
Espero que ajude.
Segue um exemplo:
unit Main;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TMainForm = class(TForm) SubtraiButton: TButton; Edit1: TEdit; Edit2: TEdit; ResultadoEdit: TEdit; Label1: TLabel; Label2: TLabel; procedure FormCreate(Sender: TObject); procedure SubtraiButtonClick(Sender: TObject); private { Private declarations } public function HorasDecimaisToStr(Qtd: Extended): string; function StrToHorasDecimais(const QtdHoras: String): Extended; { Public declarations } end;var MainForm: TMainForm;implementation{$R *.dfm}uses Math;procedure TMainForm.FormCreate(Sender: TObject);begin Edit1.Text := '35:34'; Edit2.Text := '2:57'; ResultadoEdit.Text := 'nc';end;function TMainForm.HorasDecimaisToStr(Qtd: Extended): string;var h, m: integer;begin h := Trunc(Qtd); m := Trunc((Qtd - h) * 61); // 61 para corrigir erro de arredondamento Result := Format('%d:%2.2d',[h,m]);end;function TMainForm.StrToHorasDecimais(const QtdHoras: String): Extended;var h, m: integer;begin h := StrToInt(Copy(QtdHoras,1,Pos(':',QtdHoras)-1)); m := StrToInt(Copy(QtdHoras,Pos(':',QtdHoras)+1, Length(QtdHoras))); Result := h + m/60;end;procedure TMainForm.SubtraiButtonClick(Sender: TObject);var T1, T2, Resultado: Extended;begin T1 := StrToHorasDecimais(Edit1.Text); Label1.Caption := FloatToStr(T1); T2 := StrToHorasDecimais(Edit2.Text); Label2.Caption := FloatToStr(T2); Resultado := T1 - T2; ResultadoEdit.Text := HorasDecimaisToStr(Resultado);end;end.
Há um erro de arredondamento, por isso dividi por 61 em vez de por 60. Talvez haja uma solução melhor.
Espero que ajude.
GOSTEI 0