Topic: Counter for the elapsed time

Hi all,

I would like to create a form with a few datetimepicker (which will show the actual time). In a textfield there will be written an extra time (in minutes or hours).
What i want is that on the form to have a textbox for each datetimepicker which will count down the elapsed time.
Example:
datetimepicker 1 ==> Time : 20:00:00
Textbox1 ==> 2:30:00
Button ==> To start the counting

A textbox which has to contain: datetimepicker1 value + textbox1 value ==> 22:30:00

A textfield which will show the elapsed time until it will be 00:00:00 then the textfield will turn green, meaning that the time has passed.

How can I achieve this?

I've read something about TTimer, but I can't figure out how to make it work.

Thank you.

Best regards,
Alin

Best regards,
Alin

Re: Counter for the elapsed time

var
time:TDateTime;
timer:TTimer;
time2:TDateTime = StrToDateTime('00:00:01');

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
  time := Form1.DateTimePicker2.Time - Form1.DateTimePicker1.Time;
  timer:=TTimer.Create(form1);
  timer.Interval := 1000;
  timer.OnTimer := @timego;
  timer.Enabled := True;
end;

procedure timego;
begin
  time := time-time2;
  Form1.Time.Caption := TimeToStr(time);
end;
Post's attachments

Attachment icon time.rar 2.87 kb, 76 downloads since 2024-05-13 

Re: Counter for the elapsed time

Dear Vladimir,

Thank you for your solution.
I still need some advice to implement this one.
I have a textbox where I will enter minutes.
I want to add the currentdateTime + textbox.text and assign them to datetimepicker2 (according your example) and only then to run the start button.
How can I convert text value into time and assign it to the textbox and then to make the calculation?

Thank you once again for your help.

BR,
Alin

Best regards,
Alin

Re: Counter for the elapsed time

m.alin wrote:

Dear Vladimir,

Thank you for your solution.
I still need some advice to implement this one.
I have a textbox where I will enter minutes.
I want to add the currentdateTime + textbox.text and assign them to datetimepicker2 (according your example) and only then to run the start button.
How can I convert text value into time and assign it to the textbox and then to make the calculation?

Thank you once again for your help.

BR,
Alin

Post's attachments

Attachment icon time.rar 3.26 kb, 62 downloads since 2024-05-15 

Re: Counter for the elapsed time

Hello Vladimir,

I've wrote some remarks in the attached project, what I would like to achieve and how I get the data.
I know that is not too complicated, but I really don't know how to convert the minutes I get from the database into time.

procedure Form1_Edit2_OnExit (Sender: TObject);
begin
    // this field will be populated from the database, where I have the times in minutes (like 90 minutes = 1.5 hours, 120 minutes = 2 hours, etc
  //How can I convert the minutes into hours or add those minutes to the datetimepicker2 (endtime)?
  //I tried this, but is not working
  Form1.DateTimePicker2.Time := NOW + strToDateTime(form1.Edit2.text)/60;  // here I want to get the time from the database (in minutes) and divide it my 60 minutes to get the hours and then add it to Form1.DateTimePicker2.Time
end;

Post's attachments

Attachment icon time_request.7z 274.43 kb, 77 downloads since 2024-05-16 

Best regards,
Alin

Re: Counter for the elapsed time

Don't you want to read the theory first?

TDateTime is stored as a double, the integer part representing days and the fractional part being fraction of a day. The integer part is the number of days that have passed since December 30, 1899, and can be a negative number. The fractional part reflects the fraction of a 24-hour day without regard to the sign of the TDateTime value, so care must be taken when computing negative TDateTime values with a fractional part.
Methods exist in Sysutils (datetimeroutines) that allow reading, writing and calculating with TDateTime, converting to and from a number of formats.
The following table displays examples of TDateTime values and their corresponding dates and times:
Double Value     'dd-mm-yyyy hh:nn:ss'
-1.75                      29-12-1899 18:00:00
-1.5                      29-12-1899 12:00:00
-1.25                      29-12-1899 06:00:00
0                              30-12-1899 00:00:00
1.25                      31-12-1899 06:00:00
2.25                      01-01-1900 06:00:00
36526                      01-01-2000 00:00:00
44927                      01-01-2023 00:00:00

https://wiki.freepascal.org/TDateTime

Re: Counter for the elapsed time

Hello Sparow,

Thank you, I will study a bit more.
Still looking to the link, I haven't figured out how to convert the integer value into time (on the website is more specific to compare dates and not times).
If you have any suggestion, I would appreciate to see it.

Just a short information. My main job is not as a programmer, I am trying to learn by doing for a small project I'm trying to build up.
So, I have seen here that we can ask questions, and if someone knows a solution, it helps the newbies smile

Thanks again for your feedback.

BR,
Alin

Best regards,
Alin

Re: Counter for the elapsed time

You know, I’m not a programmer either, but that doesn’t stop me from reading and learning. Any job in any field requires some knowledge.
If you don’t want to receive knowledge, then they pay money for work that someone else will do.
Questions on the forum/forums assume that the questioner has already looked up the theory, searched the forum/forums, and has not found the answer.
You could find the answer to your question here or in another forum. This topic has been discussed more than once.

Now regarding time. The date format consists of an integer part in days and a fractional part in parts of the day (hours, minutes, seconds, milliseconds).

For example, 1.25 is one day and six hours in the TDateTime representation. How to get six hours in this view? 6 divided by 24. You get 0.25.
1 + 0.25 = 1.25 (one day and six hours)
With minutes, we still divide 30 minutes by 24 (hours in a day) and divide by 60 (minutes in an hour). We get approximately 0.02084.
1 + 0.25 + 0.02084 = 1.27084 (one day, six hours, thirty minutes).
Homework for you is to calculate the seconds.


Value not only your time but also the time of other people.

Re: Counter for the elapsed time

Dear Sparrow,

Thank you for clarification.
I don't want to waste anyone's time, I'm just asking for help....
Perhaps I've made a mistake in the beginning because I haven't attached any project, but I wanted to see how the experienced developers are dealing with such demands.
For this time, I have attached my project, perhaps like this is much more easier to understand what I want to achieve.

So...in a few words...
I will have 10 dryers, for the moment, I'm building the layout only for one and then adjust it for the other 9 dryers.
The operator should press the button and activate the dryer, then scan a production order and automatically the material No and Material name will be filled up and also the drying time (in minutes). The connections to the database will be made with MYSQL (not yet made) because of testing purpose.

He then should only press the START Button and the counter should count down the remaining time.

When the time will reach : '00:00:00' the counter should stop and throw a messagebox "Material dried".

In the project, there is a textbox on the right side of the 2 datetimepicker which will hold the integer value (the minutes from the database).

I can't find any search button on this forum, maybe I'm too dumb sad
Thank you.
Alin

Post's attachments

Attachment icon Thor_uscare.zip 338.5 kb, 55 downloads since 2024-05-16 

Best regards,
Alin

Re: Counter for the elapsed time

The search link for this forum is at the very top right of the page 3rd from the right, 4th from the left wink

Re: Counter for the elapsed time

tcoton wrote:

The search link for this forum is at the very top right of the page 3rd from the right, 4th from the left wink

Umbelievable, I haven't seen it....
Thanks Tcoton wink

Best regards,
Alin

Re: Counter for the elapsed time

не понимаю зачем вам даты  и зачем вы пытаетесь преобразовывать минуты.

var
time:TDateTime;
timer:TTimer;
time2:TDateTime = StrToDateTime('00:00:01');


procedure Form1_OnShow (Sender: TObject; Action: string);
begin
  Form1.ComboBox1.Items.Add('30 minutes');
  Form1.ComboBox1.Items.Add('60 minutes');
  Form1.ComboBox1.Items.Add('90 minutes');
  Form1.ComboBox1.Items.Add('120 minutes');
end;

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
  CASE Form1.ComboBox1.ItemIndex OF
   0: time := StrToTime('00:30:00');
   1: time := StrToTime('01:00:00');
   2: time := StrToTime('01:30:00');
   3: time := StrToTime('02:00:00');
  end;
  timer:=TTimer.Create(form1);
  timer.Interval := 1000;
  timer.OnTimer := @timego;
  timer.Enabled := True;
end;

procedure timego;
begin
  time := time-time2;
  Form1.Edit1.Text := TimeToStr(time);
end;

и второй вариант

var
time:TDateTime;
timer:TTimer;
time2:TDateTime = StrToDateTime('00:00:01');


procedure Form1_OnShow (Sender: TObject; Action: string);
begin
  if (SQLExecute('SELECT COUNT(id) FROM time') = 0) then
  begin
    SQLExecute('INSERT INTO time(name,time)VALUES("30 minutes","00:30:00")'+
    ',("60 minutes","01:00:00"),("90 minutes","01:30:00"),("120 minutes","02:00:00")');
    UPDATEDATABASE('time');
  end;

end;

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
  time := SQLDateTimeToDateTime(SQLExecute('SELECT time FROM time WHERE id='+Form1.ComboBox1.sqlValue));
  timer:=TTimer.Create(form1);
  timer.Interval := 1000;
  timer.OnTimer := @timego;
  timer.Enabled := True;
end;

procedure timego;
begin
  time := time-time2;
  Form1.Edit1.Text := TimeToStr(time);
end;
Post's attachments

Attachment icon time_request.rar 7.67 kb, 58 downloads since 2024-05-17 

Re: Counter for the elapsed time

Hi M.Alin, Vladimir,
As soon as you attached your project, things became clearer as to what you are trying to do.
I used Vladimir's example (большое спасибо Vladimir) and messed around with your project.
I changed the layout quite a lot but only because I wanted to try and get all 10 dryers on one form but it's just cosmetic.
I've held the drying times in a 'materials' table and these, together with the material details, pass through to Form1 when an Order is selected as you explained in your message.
To save space, I combine the timers with the button captions.
When the timers reach 00:00, a message is displayed and the details are updated in the orders table (click on the 'configuration' button on Form1 to see details of the materials and orders that have been set up).
After that, it's just a big 'cut and paste' exercise!
The only problem I can foresee is if your drying times are greater than 24 hours (I don't know if that's actually the case so didn't allow for it in the attachment).
Maybe this gives you some ideas and helps to move your project forward.
Regards,
Derek.

Post's attachments

Attachment icon Timpi Uscare Materiale.zip 464.2 kb, 52 downloads since 2024-05-17 

Re: Counter for the elapsed time

Hi Derek, Vladimir,

You rock!
Sorry for my late reply, but I want to thank you for sharing this approach. It might help other users as well.

Thank you once again.

Best regards,
Alin

Best regards,
Alin