Solved

Delphi Countdown Timer

Posted on 1999-01-25
8
4,257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

615 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