## Recommended Posts

Good Day,
Delphi 10.3

procedure TForm2.Button1Click(Sender: TObject);
var
ST,ET : TDateTime;
Diff : Double;
begin
ST := StrToDateTime (Edit1.Text); // Text = 08:00
ET := StrToDateTime (Edit2.Text); // Text = 11:30

Diff := HoursBetween(ST,ET);
Edit3.Text := FloatToStr(Diff);

The result of above proc is 3

i was expecting  3,5

Thank You

end;

Thank you so much Tom, Stano

Stano, i know TDateTimePicker is good solution

but i want to learn to get time difference between 2 strings

17 minutes ago, Henry Olive said:

Good Day,
Delphi 10.3

procedure TForm2.Button1Click(Sender: TObject);
var
ST,ET : TDateTime;
Diff : Double;
begin
ST := StrToDateTime (Edit1.Text); // Text = 08:00
ET := StrToDateTime (Edit2.Text); // Text = 11:30

Diff := HoursBetween(ST,ET);
Edit3.Text := FloatToStr(Diff);

The result of above proc is 3

i was expecting  3,5

That's just like HoursBetween works, the return value is an integer, so the difference is rounded to the next full hour. Use MinutesBetween and divide the result by 60 if you want the difference to be in fractional hours.

Thank you  SO MUCH  Peter, Uwe

Good Day,

ST := Start Time

ET := End Time

What if

ST:='17:30' and ET :='00:00'  (The Difference should be 6,50 but i get 17,50)

If i change '00:00' to '12:00' this time the Difference is 5,50 not 6:50

if i change '00:00' to '24:00' then this time 24:00 is not valid datetime error msg.

Thank You

Edited by Henry Olive

8 minutes ago, Henry Olive said:

Good Day,

ST := Start Time

ET := End Time

What if

ST:='17:30' and ET :='00:00'  (The Difference should be 6,50 but i get 17,50)

If i change '00:00' to '12:00' this time the Difference is 5,50 not 6:50

With the 24 hour clock 00:00 is interpreted as midnight starting the current day and 24:00 as midnight ending the day.

Thank You Peter

I edited my last post 6 minutes ago if you can check

i tried also 24:00

Here below my codes

procedure TForm2.Button1Click(Sender: TObject);
var
ST,ET : TDateTime;
Diff : Double;
begin
if (ComboBox1.Text='') or (ComboBox2.Text='') then Exit;

ST := StrToDateTime (ComboBox1.Text);  '17:30'
ET := StrToDateTime (ComboBox2.Text);   '24:00'
Diff := MinutesBetween(ET,ST) / 60;

Edit1.Text := FloatToStr(Diff);
end;

The problem is that your code is based on an assumption that the end time is after the start time, but 00:00 is the beginning of the day and so can never be used as an end time. Hard to advise you what to do because we don't know your goals, where your data is coming from, how you want to handle day boundaries etc.

Thank You so much David

User enters the StartTime & EndTime by hand ( by choosing from the combox Items)

Each ComboBoxes has  Items

17:30,

18:00

19:00

...

24:00

Between 17:30 to 24:00

Edited by Henry Olive

Start of the next day is the end of the current day, so you can write:

```var
a, b: TDateTime;
begin
a := StrToDateTime('17:30');
b := StrToDateTime('00:00') + 1; // 24:00

WriteLn(MinutesBetween(b, a)); // 390```

18 minutes ago, Henry Olive said:

Thank You so much David

User enters the StartTime & EndTime by hand ( by choosing from the combox Items)

Each ComboBoxes has  Items

17:30,

18:00

19:00

...

24:00

Between 17:30 to 24:00

They can only choose from these 14 items?  Why do you need any conversion?  Isn't it just (EndIndex - StartIndex) / 2?

Thank you so much  Z, Uwe, David

David, Yes They can only choose from these 14 items

I'm sure you you suggested me a short & clever way but

what you mean by (EndIndex - StartIndex) / 2 ?

1 hour ago, David Heffernan said:

They can only choose from these 14 items?  Why do you need any conversion?  Isn't it just (EndIndex - StartIndex) / 2?

i use about the same method in our planner (with a timeoffset and a flexible interval (as setting for the user)).

```
function TframeEditFromTill.ComboToTime: TTime;
begin
Result := TimeOf(IncMinute(0, TimeOffSet + (cmbTimeTo.ItemIndex*EditTimeInterval)));
end;

```

* TimeOffSet is a integer property, just to have the possibility to force a mininum time

* EditTimeInterval is a integer property, the interval in minutes e.g. 5, 15, 30

Edited by mvanrijnen

@Henry Olive - (EndIndex - StartIndex) / 2
17:30 - 17:30 = (0 - 0) / 2 = 0
17:30 - 18:00 = (1 - 0) / 2 = 0.5
18:00 - 18:30 = (2 - 1) / 2 = 0.5
...
17:30 - 00:00 = (13 - 0) / 2 = 6.5
23:30 - 00:00 = (13 - 12) / 2 = 0.5

• 1

```So with (48 - 1) half hour INTERVALS in each day using Combos do this

...

...

or

...

...

Do same for comboBoxStop

...

StartHour :=  (combobox.selectedIndex / (24 * 2)) * 1{full day fraction that is 48/48}

...```

Edited by Pat Foley
combo Box Fix up touched.

Thank You SO MUCH  Mvanricnen, Lars, Pat

Rx librery in it use RxDateUtil.pas