GARANTIR DESCONTO

Fórum Soma e Subtração de Datas com modificações que ajudaram mto #351425

05/01/2008

0

Cálculo de Horas sendo as horas strings]

Primeira soma Horas e segunda Subtrai Horas

function SomaHora(hora1, hora2: string): string;
var
h1, h2, m1, m2: string;
ttHor, ttMin: integer;
tthor_str, ttmin_str: string;
posi: integer;
begin
if (trim(hora1) = ´´) or (trim(hora2) = ´´) then
exit;

posi := Pos(´:´, hora1);
h1 := copy(hora1, 1, posi - 1); // recupera hora 1
m1 := copy(hora1, posi + 1, 2); // recupera minuto 1

posi := Pos(´:´, hora2);
h2 := copy(hora2, 1, posi - 1); // recupera hora 2
m2 := copy(hora2, posi + 1, 2); // recupera minuto 2

tthor := strtoint(h1) + strtoint(h2); // soma horas
ttmin := strtoint(m1) + strtoint(m2); // soma minutos

while (ttmin >= 60) do //se soma de minutos for > 60
begin // somamos +1 hora e subtraimos 60 do total de minutos
tthor := tthor + 1;
ttmin := ttmin - 60;
end;

ttmin_str := FormatFloat(´00´, ttmin); //formata com dois zeros
tthor_str := FormatFloat(´00´, tthor);

result := tthor_str + ´:´ + ttmin_str; //monta o resultado
end;

function SubtraiHora(hora1, hora2: string): string;
var
h1, h2, m1, m2: string;
ttHor, ttMin: integer;
tthor_str, ttmin_str, Menos: string;
posi: integer;
begin
if (trim(hora1) = ´´) or (trim(hora2) = ´´) then
exit;

posi := Pos(´:´, hora1);
h1 := copy(hora1, 1, posi - 1); { recupera hora 1 }
m1 := copy(hora1, posi + 1, 2); { recupera minuto 1 }

posi := Pos(´:´, hora2);
h2 := copy(hora2, 1, posi - 1); { recupera hora 2 }
m2 := copy(hora2, posi + 1, 2); { recupera minuto 2 }

if strtoint(h1) > strtoint(h2) then {compara se hora 1 > q hora 2}
tthor := strtoint(h1) - strtoint(h2); { subtrai horas }
if strtoint(h1) < strtoint(h2) then {compara se hora 1 < q hora 2}
tthor := strtoint(h2) - strtoint(h1); { subtrai horas }
if strtoint(h1) = strtoint(h2) then {compara se hora 1 = hora 2}
tthor:=0;
if strtoint(m1) > strtoint(m2) then {compara se minuto 1 > q minuto 2}
ttmin := strtoint(m1) - strtoint(m2); { subtrai minutos }
if strtoint(m1) < strtoint(m2) then {compara se minuto 1 < q minuto 2}
ttmin := strtoint(m1) - strtoint(m2); { subtrai minutos }
if strtoint(m1) = strtoint(m2) then {compara se minuto 1 = minuto 2}
ttmin:=0;

while (ttmin >= 60) do { se soma de minutos for > 60 }
begin { somamos +1 hora e subtraimos 60 do total de minutos }
tthor := tthor + 1;
ttmin := ttmin - 60;
end;

if (ttmin >= -1) then { se minutos for > -1, retiramos o sinal de -, subtraímos -1 hora e subtraímos minutos de 60 para não ficar valor negativo }
Begin
Menos:=IntToStr(ttmin);
Delete(Menos,Pos(´-´,Menos),1);
ttmin:=StrToInt(Menos);
tthor:= tthor - 1;
ttmin:= 60 - ttmin;
end;

ttmin_str := FormatFloat(´00´, ttmin); { formata com dois zeros }
tthor_str := FormatFloat(´00´, tthor);

result := tthor_str + ´:´ + ttmin_str; { monta o resultado }
end;
Para testar, coloque em um form, 3 edits e um button. Faça no onclick do button:
Edit3.Text := SomaHora(Edit1.Text, Edit2.Text);

Agora é só adaptar ao que for de sua necessidade!


Tatiane

Tatiane

Responder

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar