[CLOSED] Time gap between two TDateTimes lazarus / 21-General ---------------------------------------------------------------------- egsuh · 2023-07-25 07:38 With followings, var     T1, T2: TDateTime; How can I calculate time gap between the two? e.g. T2 - T1  in minutes, seconds, milliseconds, days, etc. ? ---------------------------------------------------------------------- aydın · 2023-07-25 07:56 You can use SecondsBetween for this. It's in DateUtils. There are also derivatives such as milliseconds, minutes and hours. All here: https://www.freepascal.org/docs- html/rtl/dateutils/secondsbetween.html ---------------------------------------------------------------------- Thaddy · 2023-07-25 08:46 The above answer is correct, but you can also substract the values and call datetimetostr to obtain all values you need. This does not rely on DateUtils. ---------------------------------------------------------------------- wp · 2023-07-25 08:52 Quote from: Thaddy on July 25, 2023, 10:46:24 amThe above answer is correct, but you can also substract the values and call datetimetostr to obtain all values you need. This does not rely on DateUtils. This returns a string, which maybe is not what the OP needs. If you do not want to use DateUtils, you can do the simple maths yourself: TDateTime values are measured in days with the fractional part being the time during the day (0.0 = midnight, 0.5 = noon, etc). So, when you calculate the difference T2-T1 you get the number of days between them. Round it to an integer to get the number of full days. Multiply the (unrounded) difference by the number of seconds per day (24 hours per day * 60 minutes per hour * 60 seconds per minute), and round again, and you'll have the number of seconds between T1 and T2. ---------------------------------------------------------------------- KodeZwerg · 2023-07-25 08:59 OP can use either string as result or modify to export years days hours etc... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; uses  SysUtils, DateUtils; // i just included DateUtils to have EncodeDateTime method available function DateDiffToStr(const ADate1, ADate2: TDateTime): string;var  Years, Days, Hours, Minutes, Secs: Integer;  Difference: Real;begin  if ADate2 > ADate1 then    Difference := ADate2 - ADate1  else    Difference := ADate1 - ADate2;  Days := Round(Difference - Frac(Difference));  Years := Days div 365;  Days := Days - (Years * 365);  Difference := Frac(Difference) * 24;  Hours := Round(Difference - Frac(Difference));  Difference := Frac(Difference) * 60;  Minutes := Round(Difference - Frac(Difference));  Difference := Frac(Difference) * 60;  Secs := Round(Difference - Frac(Difference));  Result := Format('%d years, %d days, %d hours, %d minutes, %d seconds.', [Years, Days, Hours, Minutes, Secs]);end; var  date1, date2: TDateTime;begin  date1 := Now;  date2 := EncodeDateTime(1996, 3, 16, 12, 13, 14, 15);  WriteLn(DateDiffToStr(date1, date2));  ReadLn;end. ---------------------------------------------------------------------- egsuh · 2023-07-25 09:22 Thanks a lot. DateUtils is good enough. ---------------------------------------------------------------------- Чебурашка · 2023-07-25 10:04 Along with the suggested "-sBetween", DateUtiles also offers the "-Span" functions, that return the double value, not the integer. So if A is now and B is now + 3.512 secods SecondSpan(A, B) = 3.512; SecondsBetween(A, B) = 3; The real gap is expressed by the -Span version, even if in many cases the -sBetween is useful, for example to show approximate gaps. ---------------------------------------------------------------------- Thaddy · 2023-07-25 10:09 Quote from: wp on July 25, 2023, 10:52:26 amThis returns a string, which maybe is not what the OP needs. The point I made it that it is a simple subtraction, nothing more. I just added DateTimeToStr for representation. ---------------------------------------------------------------------- egsuh · 2023-07-25 10:55 Yes, I'll review them in more detail because sometimes I need at millisecond level, sometimes number of days, etc. ---------------------------------------------------------------------- KodeZwerg · 2023-07-25 11:48 For my example, you can modify last lines like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Difference := Frac(Difference) * 1000;  MSecs := Round(Difference - Frac(Difference));  Result := Format('%d years, %d days, %d hours, %d minutes, %d seconds, %d milliseconds.', [Years, Days, Hours, Minutes, Secs, MSecs]);and of course add MSecs in the var section. That would list also the milliseconds. ---------------------------------------------------------------------- toby · 2023-07-25 19:19 program timing; uses sysutils; var i, tst : longint;     tot : int64;     now1, now2 : tdatetime; procedure dn; var hour, minute, second, sec100 : word; begin //now1 := now; // put above code to test in *.pas pgm now2 := now; decodetime((now2-now1), hour, minute, second, sec100); writeln('timing : ', hour, ':', minute, '.', second, '.', sec100); writeln; end; begin writeln; tst := 1000000000; //tst := 1; tot := 0; now1 := now; for i := 1 to tst do tot := tot + 1; writeln('tot : ', tot); dn; end. ---------------------------------------------------------------------- bytebites · 2023-07-25 23:18 https://wiki.freepascal.org/Forum ---------------------------------------------------------------------- jamie · 2023-07-25 23:22 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program timing; uses sysutils; var i, tst : longint;    tot : int64;    now1, now2 : tdatetime; procedure dn;var hour, minute, second, sec100 : word;begin//now1 := now; // put above code to test in *.pas pgmnow2 := now;decodetime((now2-now1), hour, minute, second, sec100);writeln('timing : ', hour, ':', minute, '.', second, '.', sec100);writeln;end; begin writeln; tst := 1000000000;//tst := 1; tot := 0; now1 := now;for i := 1 to tst do tot := tot + 1;writeln('tot : ', tot);dn; end.  Now Now now.. ---------------------------------------------------------------------- wp · 2023-07-26 13:53 Please be careful with these pre-built functions. Understand exactly what they do. What is the meaning of "between"? Including limits? Excluding limits? Including first limit and excluding last limit? Example: This working week began on Monday July 24 and will end on Friday July 28. These are 5 working days. But when you calculate the difference by means of DaysBetween you will be off by one! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses  SysUtils, DateUtils;var  d1, d2: TDateTime;begin  d1 := EncodeDate(2023, 07, 24);  d2 := EncodeDate(2023, 07, 28);  WriteLn(DaysBetween(d2, d1));     // ---> 4  ReadLn;end. ---------------------------------------------------------------------- Чебурашка · 2023-07-26 14:12 Quote from: wp on July 26, 2023, 03:53:12 pmPlease be careful with these pre-built functions. Understand exactly what they do. What is the meaning of "between"? Including limits? Excluding limits? Including first limit and excluding last limit? I also think that it can be misleading sometimes. In reality one shoud simply assume that EncodeDate(2023, 07, 24) is not the day, like we humans intend it, the time interval between 00:00:00 and 23:59:59.999999, but is simply a point in time (in this case 00:00:00), so for a point in time there is no ambiguity about including or excluding the limit, as there is no difference except for a point that has 0 duration. ---------------------------------------------------------------------- wp · 2023-07-26 15:49 Exactly, that's the point! ---------------------------------------------------------------------- dseligo · 2023-07-26 20:55 Quote from: wp on July 26, 2023, 03:53:12 pmPlease be careful with these pre-built functions. Understand exactly what they do. What is the meaning of "between"? Including limits? Excluding limits? Including first limit and excluding last limit? Example: This working week began on Monday July 24 and will end on Friday July 28. These are 5 working days. But when you calculate the difference by means of DaysBetween you will be off by one! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses  SysUtils, DateUtils;var  d1, d2: TDateTime;begin  d1 := EncodeDate(2023, 07, 24);  d2 := EncodeDate(2023, 07, 28);  WriteLn(DaysBetween(d2, d1));     // ---> 4  ReadLn;end. Not only pre-built functions. You suggested in post #3 that he can calculate difference as T2-T1. If he calculate difference d2 - d1 in example in this post he will also get 4: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i WriteLn(d2 - d1);