Hello,
I am trying to make a countdown timer which counts down from 48 hours.
When the program is ran for the first time, the current date and time is assigned to the
TargetTime variable. This string is then stored as a new registry value.
This string will never change throughout the program's life-cycle, so if the countdown is at 36:34:00 [hh:nn:ss], and the user closes the program and reopens the program exactly 5 minutes later, the countdown will read 36:29:00. And once the system time is equal or greater than the time written in the registry, the countdown will just be 00:00:00 (or negative numbers, I haven't gotten that far yet).
Here is my complete code (uses
dateutils,
registry):
procedure TForm1.FormCreate(Sender: TObject);
var
TargetTime: TDateTime;
TargetTimeString: string;
begin
TargetTime := IncDay(Now, 2);
TargetTimeString := DateTimeToStr(TargetTime);
with TRegistry.Create do try
RootKey := HKEY_CURRENT_USER;
OpenKey('\Software', False);
if not ValueExists('TargetTime') then begin
WriteString('TargetTime', TargetTimeString);
end;
finally
Free;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
TargetTime: TDateTime;
TargetTimeString: string;
TimeToTarget: TDateTime;
begin
with TRegistry.Create do try
RootKey := HKEY_CURRENT_USER;
OpenKey('\Software', False);
TargetTimeString := ReadString('TargetTime');
finally
end;
TargetTime := StrToDateTime(TargetTimeString);
// Thanks to lucamar for the following code:
if Time > TargetTime then begin
Timer1.Enabled := False;
end else begin
TimeToTarget := TargetTime - Time;
// FormatDateTime('dd:hh:nn:ss', TimeToTarget);
Label1.Caption := TimeToStr(TimeToTarget);
end;
end;
The problem is, however many days I increment
now by (line 6), although the value will be correct in the registry, the timer doesn't count down from that value (in [hh:nn:ss]) - it will always count down from 24 hours (in [hh:nn:ss]) instead. I need it to count down from 48 hours (hence incrementing it by 2)
Why will it never show more than 24 hours?