toufik 2 Posted January 1, 2022 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
FPiette 382 Posted January 1, 2022 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. 1 Share this post Link to post
toufik 2 Posted January 1, 2022 (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 January 1, 2022 by toufik Share this post Link to post
FPiette 382 Posted January 1, 2022 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. 1 Share this post Link to post
toufik 2 Posted January 1, 2022 @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 Share this post Link to post
PeterBelow 238 Posted January 1, 2022 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. 1 Share this post Link to post
Attila Kovacs 629 Posted January 1, 2022 just use the 2 TDateTime values, start/stop and use System.DateUtils.SecondsBetween(start, stop). 1 1 Share this post Link to post
Angus Robertson 574 Posted January 1, 2022 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 1 1 Share this post Link to post
Attila Kovacs 629 Posted January 1, 2022 (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 January 1, 2022 by Attila Kovacs 1 Share this post Link to post
toufik 2 Posted January 2, 2022 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
KenR 29 Posted January 2, 2022 Not sure how you are using this but using time rather than now will give you problems if starttime/endtime spans midnight. 1 Share this post Link to post
David Heffernan 2345 Posted January 2, 2022 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 1 Share this post Link to post
toufik 2 Posted January 3, 2022 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
toufik 2 Posted January 3, 2022 and its start counting all over next time the user lunch the app and so on ... Share this post Link to post
David Heffernan 2345 Posted January 3, 2022 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. 1 Share this post Link to post
corneliusdavid 214 Posted January 4, 2022 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. 1 Share this post Link to post
toufik 2 Posted January 6, 2022 @David Heffernan i end up taking your advice Share this post Link to post