Solved

Delphi Countdown Timer

Posted on 1999-01-25
8
4,184 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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