954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Calculating the difference between 2 times regardless of the days passed

Hi

i am very new to delphi and have only been working with it for a few months, i need to calculate the difference between 2 TDateTime Variables regardless of the number of days passed.

Currently im using this:

var
  StartTime, EndTime, TimeDiff: TDateTime;
begin
  StartTime := (Now);
  EndTime := StrToTime(frm_vars.EndTime1.Caption)
  TimeDiff := EndTime - StartTime

  Label1.Caption := TimeToStr(TimeBetween);
end;


now i understand that this code will only work up until 24:00:00, i need to make this piece of code so that it calculates the time difference over 24 hours in hh:mm:ss format.

could somebody show me how to do this please.

redrobby02
Newbie Poster
6 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

What about using TTimeSpan?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

isnt TTimeSpan only usable under Delphi.Net or am i wrong?

i am struggling to find any solid information on the internet of how to actually use it, but it sounds like it is exactly what i need

How could i use TimeSpan with the current system time and another time which is a label caption

redrobby02
Newbie Poster
6 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Well, (if you're interested in Delphi.net or doe net in general), I've seen this:
http://www.delphibasics.co.uk/NameSpace.asp?Name=System&Part=TimeSpan
http://msdn.microsoft.com/en-us/library/269ew577(vs.71).aspx

[non dot net]
I also saw this conversation online that just says to subtract one from the other and get the result.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

The TDateTime is a Double Type.
The fractional part handles the time only.

VAR
  td,t :TDateTime;
BEGIN
  td=Now; // Both Time and Date in one double.
  t:=FRAC(td); // Variable t now holds Only the Time portion.
  IF t<0 THEN t:=t*-1 // If t is negative then make it positive.
  // The time is never a negative number, but the date can be. 
END.


This tiny example should give you the input needed.

your existing code work fine, but you must take away the date information and make sure negative numbers is made positive.

Best regards.

Morten Brendefu
Light Poster
44 posts since Mar 2011
Reputation Points: 38
Solved Threads: 2
 

hi

thanks for your replys but i still cant achieve the result i want.

i will try and explain it a different way.

when the app closes the time is logged and read when it is opened again. I want to compare the current system time with that value and display how long the app has been closed for in hh:mm:ss format.

if i change my code to this instead so im using just Time Values instead of DateTime does that make it any easier?

var
  StartTime, EndTime, TimeBetween: TDateTime;
begin
  StartTime := (Time); // Current System Time
  EndTime := StrToTime(EndTime1.Caption); // Time Software was closed
  
  TimeBetween := ??
  Label1.Caption := TimeToStr(TimeBetween);
end;


if i use just Time Values how can i get the difference in hh:mm:ss format between the 2.

i cant just simply subtract the values like in my first example because it needs to be able to count more than just 24 hours.

Kind Regards

redrobby02
Newbie Poster
6 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Hmmmm.. First of all.
Delphi and its Time routines does not handle more than 24 hours with TimeToStr.
At least I do not think so. I must admit that I have not tested this :-)

Anyway. The fractional part, FRAC(DateTime) holds the time only.
The Integer part, holds the date only.

I think you should try to write your own TimeToStr function.
Something like

FUNCTION FullTimeToStr(StartDT,EndDT:TDateTime):STRING;
  VAR
    sTemp            : STRING;
    Days,Hours,iTemp : INTEGER;
    TimeStart,TimeEnd: TTime;
  BEGIN
    TimeStart:= StartDT;
    TimeEnd  := EndDT;
    IF TimeStart<0 THEN TimeStart:=TimeStart*-1; // Makes negative number positive.
    IF TimeEnd<0 THEN TimeEnd:=TimeEnd*-1; // Makes negative number positive.
    sTemp    := TimeToStr(TimeStart-TimeEnd);
    iTemp    := StrToInt(Copy(sTemp,1,2));
    Days     := INT(EndDT)-INT(StartDT);
    IF Days <0 Then Days := Days *-1; // Makes negative number positive.
    Hours:=Days*24+iTemp;
    Result:=IntToStr(Hours)+COPY(sTemp,3,5);
  END;


I am a little bit unsure of the way I use INT on a value that is a double. Might get an error on that one, but the code should at least give you some ideas.
Good luck.

Morten Brendefu
Light Poster
44 posts since Mar 2011
Reputation Points: 38
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: