Summing up Time (eq. TIME + TIME = TOTAL TIME)

How can i sum up time?
eq.

 8:00 + 8:00 + 8:00 = 24:00 hrs.
 8:30 + 8:10 + 8:10 = 24:50 hrs.
 8:45 + 8:15 + 8:00 = 25:00 hrs.
 8:30 + 8:30 + 8:30 = 25:30 hrs.

Any body can help?
girlswants_meAsked:
Who is Participating?
 
Pierre CorneliusConnect With a Mentor Commented:

The strToTime function returns a variable of type TDateTime. A TDatetime type variable in delphi represents time as follows:
- The integer part is the number of days that have passed since 30-Dec-1899
- The fractional part is the fraction of a 24 hour day which has passed

So, 8 hours is represented by a TDateTime value of 0.33333333333333 because 8 / 24 = 0.333333333333

So, if you want to know the time elapsed between 2 DateTime variables you could do something like this:

procedure TForm1.Button2Click(Sender: TObject);
var Date1,Date2: TDateTime;
    s: string;
    days,hr,min: integer;
begin
  Date1:= Now - 1.258; //i.e. 1 and a quarter days earlier
  Date2:= Now;
  days:= Trunc(Date2-Date1);
  hr:= Round(Frac(Date2-Date1)*24);
  min:= Round(Frac(Date2-Date1)*24*60)-hr*60;
  s:= IntToStr(days)+' days '
               + IntToStr(Round(Frac(Date2-Date1)*24*60))+'Minutes';
  s:= IntToStr(Trunc(Date2-Date1) )+' days '#13
               + IntToStr(hr)+' hours'#13
               + IntToStr(min)+ 'minutes';
  ShowMessage(s);
end;
0
 
girlswants_meAuthor Commented:
Anybody?
0
 
girlswants_meAuthor Commented:
By the way, prioritize this one....

 a,b,c:Float;
 Total:String;

 a=strtotime('8:30);
 b=strtotime('8:30');
 c=strtotime('8:30');

 if a+b+c the answer should be '25:30 hrs' In the TOTAL

 The problem is this, I want to get the total hours rendered by an employee for a week.
 example: if an employee works 8 hours a day and 40 hours a week. this should be
  8:00 + 8:00 + 8:00 + 8:00 + 8:00 = 40:00 HOURS.

aNYBODY?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
girlswants_meAuthor Commented:
What becomes 8:00 in time 0.333333333333333 in float? Why is 8:00 = 0.333333333333333 in float? How does 0.333333333333333 becomes 8:00 in time?
0
 
webtransCommented:
well i don't know any delphi but i will search google for you
but for the time being
can't u just split the time 8:30=8 and 30
and then 8:30=8+(30/60)
and so on
i guess this can solve ur problem for the time being at least
0
 
webtransCommented:
did it help?
0
 
Ivanov_GCommented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type

  TMyTime = record
    Hours   : DWord;
    Minutes : DWord;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function SumTimes(Time1, Time2 : TMyTime) : TMyTime;
var
  tmp    : DWord;
begin
  Result.Minutes := Time1.Minutes + Time2.Minutes;
  tmp := Result.Minutes div 60;
  Result.Hours := Time1.Hours + Time2.Hours;
  if tmp > 0 then
    Result.Hours := Result.Hours + tmp;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Time1, Time2, Time3 : TMyTime;
begin
  Time1.Hours   := 8;   Time2.Hours   := 8;
  Time1.Minutes := 0;   Time2.Minutes := 0;
  Time3 := SumTimes(Time1, Time2);
  ShowMessage(IntToStr(Time3.Hours) + ':' + IntToStr(Time3.Minutes));
end;

end.
0
 
Ivanov_GCommented:
opps, I implemented it for 2 times, but the same idea you can use to sum 3 times...
0
 
girlswants_meAuthor Commented:
What becomes 8:00 in time 0.333333333333333 in float? Why is 8:00 = 0.333333333333333 in float? How does 0.333333333333333 becomes 8:00 in time?
0
 
girlswants_meAuthor Commented:
I have this

 HRS      MINS
  72       108
 
How will i convert the 108 mins to hours? is it
 108 / 60 = 1.8
Where is the hours there and the minute?
0
 
mottorCommented:

8 H = 8/24 = 1/3 = 0.33333333333333333
1.00 = 1 Day = 24 Hours

procedure TForm1.Button1Click(Sender: TObject);
var t1,t2: TDateTime;
begin
  t1 := 8/24 +30/(24*60);   This is 8:30
  t2 := 8/24 +45/(24*60);   This is 8:45
  Label1.Caption := TimeToStr(t1+t2)
end;
0
 
mottorCommented:
108 M = 108/(24*60) Days
and then use TimeToStr
if you want hours only  trunc(t1*24)


Best Regards
Mottor
0
 
esoftbgCommented:
unit Unit1_Q_21113312;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    EditSumTime: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private    { Private declarations }
  public     { Public declarations }
    TimeArray: array of string;
    function Sum_Up_Minutes: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.Sum_Up_Minutes: string;
var
  B:     Boolean;
  C:     Integer;
  I:     Integer;
  P:     Integer;
  HI:    Integer;
  MI:    Integer;
  MINS:  Integer;
  HS:    string;
  MS:    string;
begin
  MINS := 0;
  try
    I := Length(TimeArray);
    for I := 0 to I-1 do
    begin
      P := Pos(':', TimeArray[I]);
      HS := Trim(Copy(TimeArray[I], 1, P-1));
      MS := Trim(Copy(TimeArray[I], P+1, 2));
      Val(HS, HI, C);
      B := (C=0);
      if B then
      begin
        Val(MS, MI, C);
        B := (C=0);
        if B then
        begin
          MINS := MINS + HI*60 + MI;
        end;
      end;
    end;
  finally
    Result := IntToStr(MINS div 60) + ':' + IntToStr(MINS mod 60);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetLength(TimeArray, 3);
  TimeArray[0] := '08:30';
  TimeArray[1] := '08:10';
  TimeArray[2] := '08:10';
  EditSumTime.Text := Sum_Up_Minutes;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SetLength(TimeArray, 0);
end;

end.
0
 
mottorCommented:
Look also the help for
DecodeTime
0
 
kretzschmarCommented:
have some routines at home, will provide it later
0
 
esoftbgCommented:
> HRS      MINS
>  72       108

var
  MINSUM: Word;
begin
  MINSUM := HRS * 60 + MINS;
  S := (MINSUM div 60) + ':' + (MINSUM mod 60);
end;
0
 
Lee_NoverCommented:
why do it the easy way when you can complicate it right ? ugh ... might as well write it in asm

anyway:

uses DateUtils;

procedure TForm1.SomeMethod;
var TimeAll, TimeOne, TimeTwo, TimeThree: TTime;
    Hours, Minutes: Integer;
begin
     // just some random times
     TimeOne:=EncodeTime(Random(9), Random(60), 0, 0);
     TimeTwo:=EncodeTime(Random(9), Random(60), 0, 0);
     TimeThree:=EncodeTime(Random(9), Random(60), 0, 0);
     // just add them up
     TimeAll:=TimeOne + TimeTwo + TimeThree;
     Hours:=HoursBetween(TimeAll, 0);
     Minutes:=MinutesBetween(TimeAll, 0) - (Hours * 60);
     Caption:=Format('%d:%.2d', [Hours, Minutes]);
end;
0
 
Wim ten BrinkSelf-employed developerCommented:
Convert the times to TDateTime types, add them up, then convert the result back to string... Here's a function to sum up a list of times:

function SumOfTimes( const Times: array of string ): string;
var
  I: Integer;
  Total: TDateTime;
  H, M, S, MS: Word;
begin
  Total := 0;
  for I := Low( Times ) to High( Times ) do
    Total := Total + StrToTime( Times[ I ] );
  if ( Total >= 1 ) then begin
    DecodeTime( Total, H, M, S, MS );
    H := H + 24 * Trunc( Total );
    if ( M < 10 ) then begin
      Result := IntToStr( H ) + ':0' + IntToStr( M );
    end
    else begin
      Result := IntToStr( H ) + ':' + IntToStr( M );
    end;
  end
  else begin
    Result := TimeToStr( Frac( Total ) ) + ' hrs.';
  end;
end;

Usage: S := SumOfTimes( [ '8:00', '8:00', '8:00' ] );

Quite simple, actually.
0
 
kretzschmarCommented:
well, my small sample
just to say, that i don't use the TDatetimeType,
because the FracPart is not exact enough for Hours and Minutes,
under some conditions a minute is missed or added

unit TimeAdding_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


//converts a Double to an TimeString
//following Conversion before decimal = Hour, frac = Minutes
//fillto adds '0' before
Function FloatToTime(AFloat : Double; fillTo : Integer = 0) : String;
var
  v : Double;
  s : String;
begin
  Result := IntToStr(Trunc(AFloat))+':';
  while length(Result)-1 < fillTo do result := '0'+result;
  v := (frac(AFloat)+0.001) * 60;
  s := IntToStr(Trunc(v));
  while length(s) < 2 do s := '0'+s;
  result := result + s;
end;

//converts a TimeString into a double
//following Conversion before decimal = Hour, frac = Minutes
//Check24 disallows higher Values than 24, if set to true
Function TimeToFloat(AString : String; Check24 : Boolean = false) : Double;
var
  p : integer;
  v : Double;
begin
  If AString = '' then
    result := 0
  else
  begin
    p := pos(':',AString);
    if p = 0 then
    try
      result := StrToFloat(AString);
    except
      raise exception.create('Invalid Timeformat');
    end
    else
    begin
      try
        result := StrToFloat(Copy(AString,1,p-1));
      except
        raise exception.create('Invalid Timeformat');
      end;
      try
        v := StrToFloat(Copy(AString,p+1,2));
        if v > 59 then
          raise exception.create('Invalid Timeformat');
        result := result + (v/60);
        if (check24) and (result > 24) then
          raise exception.create('Invalid Timeformat');
      except
        raise exception.create('Invalid Timeformat');
      end;
    end;
  end;
end;

//usage sample
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit5.text := FloatToTime(
                  TimeToFloat(edit1.text)+
                  TimeToFloat(edit2.text)+
                  TimeToFloat(edit3.text)+
                  TimeToFloat(edit4.text)
                 );
end;

end.

meikl ;-)
0
 
girlswants_meAuthor Commented:
Thanks a lot. You got it right there!

So, 8 hours is represented by a TDateTime value of 0.33333333333333 because 8 / 24 = 0.333333333333

I will sum up all real numbers (TIME) then multiply it by 24 and i will get the total hours rendered. Trancate the Hours then the remaining decimals will be divided by 60.


0
 
Pierre CorneliusCommented:
You've got it. You could also take it further into seconds.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.