Forum > Beginners

Time Difference (Minutes and seconds between)

(1/3) > >>

seghele0:
Below is a try.
Is this a correct code or is there a better programming code to determine the time between Start and End?
The program works.
Should "EpikTimer" always be used in this case?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Needs componeneet "EpikTimer" in Uses.unit Unit1;{$mode objfpc}{$H+}interface uses  Classes, SysUtils, FileUtil, Controls, Forms,  Graphics, Dialogs, StdCtrls, ExtCtrls,  EpikTimer; type   { TForm1 }  TForm1 = class(TForm)    Button1: TButton;    ETimer: TEpikTimer;    Label1: TLabel;    labelNOW: TLabel;    labelDIFFERENCE: TLabel;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);   private    { private declarations }  public    { public declarations }  end; var  Form1   : TForm1;  STARTTime : TDateTime; implementation {$R *.lfm} procedure TForm1.FormCreate(Sender: TObject);begin  StartTime := Now;  labelNOW.Caption := FormatDateTime('hh:mm:ss', StartTime);end; procedure TForm1.Button1Click(Sender: TObject);var   EndTime, TimeDifference : TDateTime;   strTimeDifference : string;begin   EndTime := Now;   TimeDifference := EndTime - StartTime;   strTimeDifference := FormatDateTime('h"hrs,"n"min,"s"sec"', TimeDifference);   labelDIFFERENCE.Caption:= strTimeDifference;end; initialization  STARTTime := Now; end. 

MarkMLl:
Why don't you just subtract two TDateTimes? It won't (realistically) rollover, so the only significant issues are (a) DST transitions and (b) that the further they move from the epoch the lower the resolution.

MarkMLl

y.ivanov:

--- Quote from: seghele0 on March 23, 2022, 04:52:33 pm ---Should "EpikTimer" always be used in this case?

--- End quote ---
Where the EpikTimer was actually used? In your code, I mean.

howardpc:

--- Quote from: MarkMLl on March 23, 2022, 04:56:29 pm ---Why don't you just subtract two TDateTimes?
--- End quote ---
Mark, do you need better glasses?
Subtracting two TDateTimes is exactly what seghele0's code does.

@y.ivanov
segehele0's question could be rephrased:
"In cases like this is EpikTimer always more accurate than subtracting TDateTimes obtained from Now()/Date()/Time() ?"

I don't know the answer, but like him would be interested to find out.

wp:

--- Quote from: seghele0 on March 23, 2022, 04:52:33 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---   TimeDifference := EndTime - StartTime;   strTimeDifference := FormatDateTime('h"hrs,"n"min,"s"sec"', TimeDifference); 
--- End quote ---
If there's any chance that TimeDifference may be larger than 24 hours then the normal FormatDateTime statement will return only the hour within a full day. Use the extended syntax with "h" in square brackets to get the correct result with the total hours

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;uses  SysUtils;var  t1, t2: TDateTime;begin  t2 := Now;  t1 := t2 - 1.5;  // t2 - t1 is 1.5 days, or 36 hours.  WriteLn(FormatDateTime('h:nn:ss', t2-t1));  WriteLn(FormatDateTime('[h]:nn:ss', t2-t1, [fdoInterval]));    ReadLn;end.

Navigation

[0] Message Index

[#] Next page

Go to full version