Solved

Delphi Countdown Timer

Posted on 1999-01-25
8
4,071 Views
Last Modified: 2009-03-16
Hi there! I am new to Delphi but learning fast! Old Turbo Pascal programmer here!!
What I want to do is to write a simple countdown timer that will show a display such as "5 days 06:04:59" until the day in question (hard coded - at midnight) is reached.. Let's say the countdown date is midnight 20th Feb, 99. I am searching everywhere but can't find any examples.. Can any coder out there show me some sample code for this and further my knowledge in the process??? Many thanks for your time.
0
Comment
Question by:stevemcd072098
  • 4
  • 2
  • 2
8 Comments
 

Author Comment

by:stevemcd072098
ID: 1363314
Edited text of question
0
 

Author Comment

by:stevemcd072098
ID: 1363315
Edited text of question
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 100 total points
ID: 1363316
Try this:

Make a form and sets its formcreate to the one below. Add a label to it (label1). Also add a timer (Timer1), set its enabled property to true and its interval to 1000 (milliseconds). Set the OnTimer to the one below.

var
  StopAt : TDateTime;
  CurrentDateTime : TDateTime;

procedure TForm1.Timer1Timer(Sender : TObject);
var
  TimeToGo : TDateTime;

begin
  TimeToGo := StopAt - Now;
  Label1.Caption := format('%d days %s', [Trunc(TimeToGo),
                                                             FormatDateTime('hh:mm:ss', TimeToGo)]);  
end;


procedure TForm1.FormCreate(Sender : TObject);

begin
  StopAt := DateToStr('1/2/99') + 1; // stop at midnight 1 Feb 1999.
end;

Cheers,

Raymond.
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1363317
There was a very similiar question about a month ago or so. He wanted a millenium countdown type of program, and I believe the answer was a reasonably complete one.
I'll dig around and see if I can find it.

Cheers,
Phil.

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:philipleighs
ID: 1363318
Did a quick scan, couldn't find it. Raymond's answer looks cool though.
0
 

Author Comment

by:stevemcd072098
ID: 1363319
Great answer - I've learnt a lot already - thanks. One thing I discovered, I swapped DateToStr for StrToDate so that it compiled ok.


I've added the following to the code to detect when time is up - is this an efficient way
of stopping the timer?? Many thanks!

procedure TForm1.Timer1Timer(Sender: TObject);
var
  TimeToGo : TDateTime;

begin
  CurrentDateTime := Now;
  if CurrentDateTime >= StopAt then
  begin
       Label1.Caption := 'TIME UP';
       Timer1.Enabled := False;
  end
  else
  begin
  TimeToGo := StopAt - CurrentDateTime;
  Label1.Caption := format('%d days %s', [Trunc(TimeToGo),
  FormatDateTime('hh:mm:ss', TimeToGo)]);
  end;
end;
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1363320
Yes this is an efficient way of stopping the timer. I forgot to put the code in to stop it - just skipped my mind :-)

Cheers,

Raymond.
0
 

Author Comment

by:stevemcd072098
ID: 1363321
Many Thanks again, Raymond for your work on this - It was EXACTLY what I was looking for. Very satisfied!!!!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now