14/02/2020 21:39:09
14/02/2020 21:39:09 [subtact] 12/02/2020 22:53:00
Currently, the string that was written to the registry (incrementing "now" by 2 days) is:OK a long since I used date time code so take a look on the code but datetime is floating point data type that has on the integer part the date and on the decimal part the time. timetostr should only try to translate the time part and ignore the date you need to multiply trunc(TimeToTarget) * 24 and add it to the final result manually or use the one of the existing functions to do it manually take a look on HoursBetween and friendsQuote14/02/2020 21:39:09
That is 100% correct.
Now, refer to line 39 of my code:
TimeToTarget := TargetTime - Time; // TargetTime being the date and time that was stored in the registry
That is, 14/02/2020 21:39:09 [subtact] 12/02/2020 22:53:00 which should output roughly 46:45:00, but it doesn't - it outputs roughly 22:45:00.
Two Days from today is 15-2-20 10:51:04
Now is 13-2-20 10:51:04
Sleeping for approx. 5 seconds
Time to target 47 hours 59 minutes 54 seconds
Program TimeTest; Uses SysUtils, DateUtils; Var TargetTime: TTime; MyNow: TTime; Hours: Int64; Minutes: Int64; Seconds: Int64; Begin TargetTime := IncHour(Now,48); Writeln('Two Days from today is ',DateTimeToStr(TargetTime)); Writeln('Now is ',DateTimeToStr(Now)); WriteLn('Sleeping for approx. 5 seconds'); Sleep(5000); MyNow := Now; Hours := HoursBetween(MyNow,TargetTime); Minutes := MinutesBetween(MyNow,TargetTime) - (Hours * 60); Seconds := SecondsBetween(MyNow,TargetTime) - ((Hours * 60 * 60) + (Minutes * 60)); WriteLn('Time to target ', Hours, ' hours ', Minutes, ' minutes ', Seconds, ' seconds'); End.
outputs:Code: [Select]Two Days from today is 15-2-20 10:51:04
Now is 13-2-20 10:51:04
Sleeping for approx. 5 seconds
Time to target 47 hours 59 minutes 54 seconds
Trev's solution is good, but in case you're still looking for an explanation, your problem was with TimeToStr(). In a TDateTime the Time part never represents more than 23:59:59.999 so TimeToStr() can never show larger intervals.Thank you for that explanation - that explains why it was never more than 24hrs.
To show them properly you have to use FormatDateTime(), for example with:
Label1.Caption := FormatDateTime('[hh]:nn:ss', TimeToTarget, [fdoInterval]);