Solved

Delphi Countdown Timer

Posted on 1999-01-25
8
4,133 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

785 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