Fórum Diferença entre horas #390287
10/11/2010
0
Felipe Ip
Curtir tópico
+ 0Posts
10/11/2010
Pietro Braga
LSaldo.Caption := FloatToStr(s-p);
Gostei + 0
10/11/2010
Felipe Ip
Gostei + 0
13/11/2010
Felipe Ip
Gostei + 0
13/11/2010
Juan Garcia
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
Clique aqui para fazer login e interagir na Comunidade :)