Search code examples
windowsdelphilazarus

Delphi calculate hours


I have a problem with delphi, I created a program that calculates working hours, this is a piece of code:

procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c,d,e,f,g,h,i,l,m,n,o,p,q,r,s,t,u,v,z,a1,b1,c1,d1,e1,f1:TTime;
begin
  a := StrToTime(Edit1.Text);
  b := StrToTime(Edit2.Text);
  c := StrToTime(Edit3.Text);
  d := StrToTime(Edit4.Text);
  e := StrToTime(Edit5.Text);
  f := StrToTime(Edit6.Text);
  g := StrToTime(Edit7.Text);
  h := StrToTime(Edit8.Text);
  i := StrToTime(Edit9.Text);
  l := StrToTime(Edit10.Text);
  m := StrToTime(Edit11.Text);
  n := StrToTime(Edit12.Text);
  o := StrToTime(Edit13.Text);
  p := StrToTime(Edit14.Text);
  q := StrToTime(Edit15.Text);
  r := StrToTime(Edit17.Text);
  s := StrToTime(Edit18.Text);
  t := StrToTime(Edit19.Text);
  u := StrToTime(Edit20.Text);
  v := StrToTime(Edit21.Text);
  z := StrToTime(Edit22.Text);
  a1 := StrToTime(Edit23.Text);
  b1 := StrToTime(Edit24.Text);
  c1 := StrToTime(Edit25.Text);
  d1 := StrToTime(Edit26.Text);
  e1 := StrToTime(Edit27.Text);
  f1 := StrToTime(Edit28.Text);
  Memo1.Text:=TimeTostr(b-a+d-c+f-e+h-g+l-i+n-m+o-n+q-p+s-r+u-t+z-v+b1-a1+d1-c1+f1-e1);
end;

end.    

But when I click on calculate, if the total exceeds 24 hours, back to 0, how do I fix this?

Thanks


Solution

  • This is how to resolve your immediate problem:

    1. Declare a new variable TotalTime the way you've declared a,b,c, etc.

    2. Then remove the last line, and replace it with this:

    .

     TotalTime := b-a+d-c+f-e+h-g+l-i+n-m+o-n+q-p+s-r+u-t+z-v+b1-a1+d1-c1+f1-e1;
     Memo1.Text:= IntToStr(Trunc(TotalTime))+' day(s), '+ TimeTostr(TotalTime);
    

    That should show something like this: 3 day(s), 07:04:45

    Besides this change, I would rethink the entire approach if I were you. Learn from what others say here, and don't let yourself be affected by downvotes or negative comments.