Jump to content
toufik

calculete time in delphi

Recommended Posts

good morning every one ,,, 
i want to count or  Calculating Elapsed time of my running Delphi application ,its start when app start .... in hour and minute and second and days 
what's the right way  to  approach  i did tried a lot of example no luck yet 

Share this post


Link to post

Is it your homework?

It would be better that you show the code you already wrote so that we can help you with it.

If we give you the solution - which is quite trivial - you won't learn anything.

  • Like 1

Share this post


Link to post
Posted (edited)
7 minutes ago, FPiette said:

Is it your homework?

It would be better that you show the code you already wrote so that we can help you with it.

If we give you the solution - which is quite trivial - you won't learn anything.

its not my homework LOL i'm very old for that , I'm Delphi Beginner that all 
and this my code 
procedure TForm1.Timer1Timer(Sender: TObject);
var  StartTime:      TDateTime;
  EndTime:        TDateTime;
  ElapsedTime:    TDateTime;
  Year, Month, Day, Hour, Min, Sec, MSec: Word;
begin
begin
  StartTime:= Time;  {get the process start time}
  {.......do processing..........}
  EndTime:= Time;    {get the process end time}
  ElapsedTime:= EndTime-StartTime;  {Total elapsed time}b.Caption :=TimeToStr(ElapsedTime)
  {DecodeTime will return total time down to the milliseconds}
  DecodeTime(ElapsedTime, Hour, Min, Sec, MSec);

Edited by toufik

Share this post


Link to post

Your code compute the elapsed time for a given calculation, not the time your application is running. Which one do you need?

 

If what you need is the time elapsed since the start of your program, use the main form OnCreate event to record the starting time and a timer to periodically compute the elapsed time (Just the difference between current time and recorded start time) and display it on screen.

 

  • Like 1

Share this post


Link to post
1 hour ago, toufik said:

@FPiette
sorry for i was bit unclear and thanks for taking from your time ;;; 
this what i was trying to do , i'm almost there  i just need to remove that 12 start from 00:00:00 not 12:00:00 any idea ?

bandicam_2022-01-01_09-51-18-033.avi

DecodeTime is the wrong method to use since it decodes a time point, not a time interval.

The System.Diagnostics.TStopwatch record may be of more use for your problem.

 

Add a private field

  FStopwatch: TStopwatch;

to your form. In the OnCreate event you start the stopwatch with

FStopwatch := TStopwatch.StartNew;

When you want to know the elapsed time call FStopwatch.Stop first and then examine the Elapsed property. It returns a TTimespan record, which has properties to dissect the time interval into days, hours, minutes etc.

  • Thanks 1

Share this post


Link to post

just use the 2 TDateTime values, start/stop and use System.DateUtils.SecondsBetween(start, stop).

 

  • Like 1
  • Thanks 1

Share this post


Link to post

You should never use TDateTime for duration calculations, users can change the system time, and summer time saving changes it twice a year (unless you use UTC time). 

 

Always use the difference between two GetTickCount64 Int64 values.

 

Angus

 

 

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Posted (edited)

As long as it's a windows application yes, use GetTickCount64.

Whereas both will count the time the computer is in sleep or in hibernation. Not sure about TStopWatch.

Edited by Attila Kovacs
  • Thanks 1

Share this post


Link to post

thank you all for help this worked for me 
var
   Year, Month, Day, Hour, Min, Sec, MSec: Word;
  StartTime:           ElapsedTime:    TDateTime;
  EndTime:= Time;
  ElapsedTime:= EndTime-StartTime; Day:= trunc(ElapsedTime);
  DecodeTime(ElapsedTime, Hour, Min, Sec, MSec);

Share this post


Link to post

Not sure how you are using this but using time rather than now will give you problems if starttime/endtime spans midnight.

  • Like 1

Share this post


Link to post
3 hours ago, toufik said:

thank you all for help this worked for me 
var
   Year, Month, Day, Hour, Min, Sec, MSec: Word;
  StartTime:           ElapsedTime:    TDateTime;
  EndTime:= Time;
  ElapsedTime:= EndTime-StartTime; Day:= trunc(ElapsedTime);
  DecodeTime(ElapsedTime, Hour, Min, Sec, MSec);

No idea why you wouldn't use TStopwatch 

  • Like 1

Share this post


Link to post

I'm using calculate time  not any longer then 8 or 7 hour a day witch is the time the user using the application that's it 
is that fine ?i hope i was clear thank you all 
@David Heffernan@KenR

Share this post


Link to post

and its start counting all over next time the user lunch the app and so on ...

Share this post


Link to post
1 hour ago, toufik said:

I'm using calculate time  not any longer then 8 or 7 hour a day witch is the time the user using the application that's it 
is that fine ?i hope i was clear thank you all 
@David Heffernan@KenR

It still doesn't make any sense to me why you wouldn't just use TStopwatch. 

  • Like 1

Share this post


Link to post
On 1/3/2022 at 9:12 AM, David Heffernan said:

It still doesn't make any sense to me why you wouldn't just use TStopwatch. 

I agree. Since I discovered TStopWatch (in the RTL's Diagnostics unit), I use it exclusively for any timing as it's so simple. It's been in Delphi at least back to XE if not before. No use writing your own timing routines anymore.

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×