• Status: Solved
• Priority: Medium
• Security: Public
• Views: 275

# Formula on computing time

for example i started in 9:30am and end in 2:45pm in that
the same date how can i get the total time used from
9:30 to 2:45, how many hours & minutes is used?
can you give me a formula for that.

specially on computing the time from this day and ends
the other day.
ex. from 7:00am todays date and end with
8:am tomorrows date.

0
mvalez
• 4
• 3
• 2
• +3
1 Solution

Commented:
declare two TDateTime type variables:  var  s, e: TDateTime
declare timeUsed:  var timeUsed: Double;
assign the startTime to s
assign endTime to e
timeUsed := (e - s) * 1436
{this give you the time used calculated in minutes, you can convert it to hour by dividing it with 60 ....  }

0

Commented:
1436?
0

Commented:
On day = 23 hours 56 minutes or 1436 minutes
0

Author Commented:

how can i compute with different dates
0

Author Commented:

how can i compute with different dates
0

Author Commented:

how can i compute with different dates
0

Commented:
mvalez,

kjteng's method works, but there's a much more straightforward way of calculating the information you want. It's to use GetTickCount instead.

The only 2 drawbacka of GetTickCount is: 1)if the machine is turned off somewhere in the middle of the process, the count is reset; 2) if the process runs continuously for over 49.7 days, the GetTickCount will also reset! However, so far, I don't believe Windows 95 is able to be run for that long (the longer my Win95 has been up is 6 days, and 29 days for the office's NT machine).

Using GetTickCount you get the difference between the first and the second call in milliseconds. Converting that to minutes, hours, days, etc, is pretty straighforward:

var
t1, tt : DWORD;
begin
t1 := GetTickCount;
// run your process for as long as 49.7 days!
tt := (GetTickCount - t1) div 1000;
// Now tt holds the elapsed time in seconds

for the elapsed time in:
seconds --> use tt
minutes:seconds --> use tt div 60 and tt mod 60

and so on.

if you prefer this solution over the one currently proposed answer, you are allowed to reject the proposed answer.

Yours,

Alex
0

Commented:
What do you mean by "different date"?
The above method should work even if e and s are not in the same day.
0

Commented:
hi all,

Var Start,Diff : TDateTime;

procedure TForm1.FormCreate(Sender: TObject);
begin
Start := Now;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Diff := Now - Start;
ShowMessage(IntToStr(Trunc(Diff))+' Days '+TimeToStr(Diff));
end;

meikl
0

Commented:
mvalez,

as we can see, there are several ways of doing what you want, because it's pretty trivial stuff. I suggest you choose the method that serves you best and let us know.

Yours,

Alex
0

Commented:
I think kretschz.. is the best and easier solution
0

Author Commented:
Thank you very much for the answer
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.