0

I am using FastReport with Delphi. I have following fields:

DATE_FROM  (example: 01.05.2015.)
TIME_FROM  (example: 13:00)

DATE_TO    (example: 02.05.2015.)
TIME_TO    (example: 10:00)

I need to calculate how many days and hours have passed between these pairs of dates. In this example it is less than 24 hours... so the result should be:

0 days   21 hours

How to achieve that?

Kromster
  • 7,181
  • 7
  • 63
  • 111
Volkan
  • 494
  • 3
  • 14
  • 32
  • 3
    I don't know if FastReports will do the sum, but you might like to consider using a calculated field in your dataset component and do the sum there. What type are the date and time fields that you are using? – Michael Vincent Jul 14 '16 at 07:46
  • You are right. In meantime, I did the cast in dataset sql CAST(DATE_FROM as datetime) + CAST(TIME_FROM as time) AS DATE_TIME_FROM Can I calculate the difference in SQL from such generated fields? Like: DATE_TIME_TO - DATE_TIME_FROM? – Volkan Jul 14 '16 at 07:49
  • with two casts in Sql I got these results: 2016-01-13 09:15:00.000 2016-01-13 14:30:00.000 – Volkan Jul 14 '16 at 07:54
  • 1
    this helped: http://stackoverflow.com/questions/23630569/date-difference-formatted-in-days-hours-minutes-seconds – Volkan Jul 14 '16 at 08:04
  • Why are you not saving an actual DATETIME value? It would make this calculation simple in your SQL. – Ken White Aug 21 '16 at 00:21

1 Answers1

-1

I would install TurboPower Orpheus. Include the OvcDate unit in your uses statement. This example was produced with Berlin 10.1 Architect and is reasonably downward compatible. There are many versions of Orpheus.

procedure TForm2.SpeedButton1Click(Sender: TObject);
var
  date1 : TDateTime;
  date2 : TDateTime;
  rdt1 : TStDateTimeRec;
  rdt2 : TStDateTimeRec;
  days : Integer;
  seconds : Integer;
  hrs : Integer;
  min : Integer;
  sec : Integer;
begin
  date1 := StrToDateTime('01/05/2015 13:00');
  date2 := StrToDateTime('02/05/2015 10:00');
  rdt1.D := DateTimeToStDate(date1);
  rdt2.D := DateTimeToStDate(date2);
  rdt1.T := DateTimeToStTime(date1);
  rdt2.T := DateTimeToStTime(date2);
  DateTimeDiff(rdt1, rdt2, days, seconds);
  hrs := seconds div SecondsInHour;
  min := ((seconds - (hrs * SecondsInHour)) div SecondsInMinute);
  sec := (seconds - (hrs * SecondsInHour) + (min * SecondsInMinute));
  sec := sec;
  // days = 30
  // seconds = 75600
  // hrs = 21
  // min = 0
  // sec = 0
end;

The above example establishes proof of concept and shows how the calculation works. Your input was 1.5.2015 and 2.5.2015 for the respective dates and that is what I used.

I would set up the appropriate calculation fields and place this formula in the OnCalc event. Be sure to establish a mechanism to set the start and stop datetime fields. Let the DBMS do the rest. I just happen to be one of the Most Popular Delphi Authors. http://cc.embarcadero.com/PopularAuthors.aspx

Arch Brooks
  • 183
  • 1
  • 3
  • 19
  • You don't need to install Orpheus to do date calculations in Delphi. It comes with the DateUtils unit that has quite a few functions for working with Delphi's TDateTime values, including DaySpan, HourSpan, DaysBetween and HoursBetween that have been available for a decade or more. And what in blazes does your Most Popular Delphi Authors have to do with a thing here? It has no relevance either to the question asked or your answer; it's just self-aggrandizement that isn't appropriate here. It's not impressive either, when you're not aware of built-in functions that do the task. – Ken White Aug 20 '16 at 23:28