Solved

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

Posted on 2004-08-31
21
505 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:girlswants_me
  • 6
  • 3
  • 2
  • +6
21 Comments
 

Author Comment

by:girlswants_me
ID: 11939405
Anybody?
0
 

Author Comment

by:girlswants_me
ID: 11939450
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
 

Author Comment

by:girlswants_me
ID: 11939523
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:webtrans
ID: 11939540
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
 
LVL 5

Expert Comment

by:webtrans
ID: 11939568
did it help?
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11939610
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
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11939617
opps, I implemented it for 2 times, but the same idea you can use to sum 3 times...
0
 

Author Comment

by:girlswants_me
ID: 11939666
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
 

Author Comment

by:girlswants_me
ID: 11939675
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
 
LVL 4

Expert Comment

by:mottor
ID: 11939713

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
 
LVL 4

Expert Comment

by:mottor
ID: 11939723
108 M = 108/(24*60) Days
and then use TimeToStr
if you want hours only  trunc(t1*24)


Best Regards
Mottor
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11939811
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
 
LVL 4

Expert Comment

by:mottor
ID: 11939825
Look also the help for
DecodeTime
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11939844
have some routines at home, will provide it later
0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 250 total points
ID: 11939996

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
 
LVL 12

Expert Comment

by:esoftbg
ID: 11940041
> HRS      MINS
>  72       108

var
  MINSUM: Word;
begin
  MINSUM := HRS * 60 + MINS;
  S := (MINSUM div 60) + ':' + (MINSUM mod 60);
end;
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 11940115
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
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11940138
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11943708
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
 

Author Comment

by:girlswants_me
ID: 11948001
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
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 11950157
You've got it. You could also take it further into seconds.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert a string into a TDateTime 5 61
Delphi...Split view - idea? 1 71
RESTRequest Parameter 4 32
Delphi: barcode reading on android platform 1 34
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question