Solved

Delphi Countdown Timer

Posted on 1999-01-25
8
4,163 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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