Buscar

Calcula diferenca de horas

Código

{Ai galera, estava em casa desenvolvendo um programa e ai pintou a duvida de calcular dif de horas, porem , com o comandinho basico do delphi , ele nao entende a diferenca dos dias.......ai eu kebrei a cabecinha e desenvolvi essa conta ai.......ve se ta legal........Abs:}


procedure TJanelaPrincipal.CalculaDfHoraExecute(Sender: TObject);
var
shoraini,shorafi:string;
rsh,rsm,rss:string;//string resultado
hr,mr,sr:integer; //integer resultado
hi,mi,si,hf,mf,sf:integer; //valores separados
begin

shorafi:=TimeToStr(RecebehoraFinal);
hf:=StrToInt(copy(shorafi,1,2));
mf:=StrToInt(copy(shorafi,4,2));
sf:=StrToInt(copy(shorafi,7,2));

shoraini:=TimeToStr(RecebeHoraInicial);
hi:=StrToInt(copy(shoraini,1,2));
mi:=StrToInt(copy(shoraini,4,2));
si:=StrToInt(copy(shoraini,7,2));

hr:=(hf-hi); //hora
mr:=(mf-mi); //minuto
sr:=(sf-si); //segundo

{ if sf<si then begin
sr:=60-si+sf; //(60-si)+sf
end;
if (mf=mi+1)and(sf<si) then begin
mr:=mr-1;
end;
if mf<mi then begin
mr:=60-mi+mf;
end;
if hf<hi then begin
hr:=24-hi+hf;
end;
if (hf=hi+1)and(mf<mi) then begin
hr:=hr-1;
end; }



if sf<si then begin
sr:=60-si+sf; //(60-si)+sf

//if (mf=mi+1) then begin
mr:=mr-1;
//end;
end;
//segundos


if mf<mi then begin
mr:=60-mi+mf;
//if (mr=1)and(sf<si)then begin
hr:=hr-1;
//end;
end;
//minutos


if hf<hi then begin
hr:=24-hi+hf;
if {(hf=hi+1)}(hr=1) and((mf<mi)or(sf<si)) then begin
hr:=hr-1;
end;
end;
//horas




if pos('-',IntToStr(hr))=1 then begin
hr:=hr-100;
rsh:=copy(IntToStr(hr),1,4);
rsh:=copy(rsh,length(rsh)-1,2);
end
else begin
hr:=hr+100;
rsh:=copy(IntToStr(hr),1,4);
rsh:=copy(rsh,length(rsh)-1,2);
end;

if pos('-',IntToStr(mr))=1 then begin
mr:=mr-100;
rsm:=copy(IntToStr(mr),1,4);
rsm:=copy(rsh,length(rsm)-1,2);
end
else begin
mr:=mr+100;
rsm:=copy(IntToStr(mr),1,4);
rsm:=copy(rsm,length(rsm)-1,2);
end;

if pos('-',IntToStr(sr))=1 then begin
sr:=sr-100;
rss:=copy(IntToStr(sr),1,4);
rss:=copy(rss,length(rss)-1,2);
end
else begin
sr:=sr+100;
rss:=copy(IntToStr(sr),1,4);
rss:=copy(rss,length(rss)-1,2);
end;

CalculaDifHoraResult:=rsh+':'+rsm+':'+rss; //setando valores

RecebehoraFinal:=0; //limpando os valores das variaveis
RecebeHoraInicial:=0;
end;

Publicidade

Vote na dica




Quantidade de votos: 0 votos
Aceitação: 0%


Detalhes da dica

Categoria: Data e hora
Adicionada dia: 27/11/07
Por: Cleberson Alves
Visualizada: 5719 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009