Jump to content
Henry Olive

HoursBetween

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;

Share this post


Link to post

Why do you use TEdit and not TTimePicker etc.? They are intended for this purpose. It's a big mistake for me!

Share this post


Link to post

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

Share this post


Link to post
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.

Share this post


Link to post

You can achieve the desired result with TTimeSpan:

  diff := TTimeSpan.Subtract(ET, ST).TotalHours;

 

Share this post


Link to post

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

Share this post


Link to post
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.

Share this post


Link to post

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;

Share this post


Link to post

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. 

Share this post


Link to post

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

Share this post


Link to post

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

 

Share this post


Link to post
9 minutes ago, zed said:

b := StrToDateTime('00:00') + 1; // 24:00

Well, that can obviously be simplified to b := 1;

Share this post


Link to post
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?

Share this post


Link to post

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 ?

 

Share this post


Link to post
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

Share this post


Link to post

@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

  • Like 1

Share this post


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



         comboBoxStart.items.add(00:00)

        ...

        comboBoxStart.items.add(12:00)

        ...

       comboxBoxstart.items.add(23:30)

      comboxBoxStart.items.add('Tomorrow');


       

     or 

      comboboxStart.items.add('yesterday')

      comboBoxStart.items.add(00:30)

        ...

       comboBoxStart.items.add(12:00)

        ...

       comboxBoxstart.items.add(24:00)



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.

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

×