A question about DateTimePicker

Dear Experts,

I have a Timer and a label on my form just to display the time.
I also have put a DateTimePicker on my form.
How can I programm that when the DateTimePicker is set at 15:00 for example
to display a message.

it has to be something like this:

IF datetimepicker.time = systemtime then
Showmessage('hello').

This is what i got:

procedure TMainForm.FormCreate(Sender: TObject);
begin
  timer1.Enabled := True;
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
  Label1.Caption := FormatDateTime('hh:mm:ss', time);
end;

Grz,

PK
LVL 1
peterkiersAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rfwoolfConnect With a Mentor Commented:
The problem with Johnjce's suggestion however is that this will not work unless the datetimepicker selects the exact same time, as the system time when the Timer1's Timer event triggers.
Example:
1. The system time is 15:00:06 (3 o'clock and 6 seconds)
2. The user selects the time 15:00:00 in the DateTimerPicker
3. The Timer1's Timer event fires, it compares 15:00:06 with 15:00:00 and because they do not match, it doesn't do anything!

There are all kinds of similar complications. Here's another complication: the Timer is set to execute every 1000 milliseconds, but that might not happen exactly every second. So when you ask it to do something at exactly 15:00:00, your timer only executes at say 15:00:00 : 500

For this reason, any DateTime comparisons must be rounded within a time span of say 1 minute or 2-3 seconds.
You can add DateUtils to your unit and use some of their functions, such as SecondsBetween. You can then say
  If SecondsBetween(DateTimePicker1, Now) < 5 then
showmessage('hi');
0
 
JohnjcesConnect With a Mentor Commented:
Try the following...

Save the datetimepicker's time in a TDateTime, i.e.
 
var
AlramDateTime : TDateTIme

On the DateTime Picker's change event add

AlarmDateTime := DateTimePicker.Time

in your timer, set to say one second., do..

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
  if AlarmDateTime = Now then
   Label1.Caption := FormatDateTime('hh:mm:ss', time);
end;

Hope this gets you started.

John
0
 
JohnjcesCommented:
RT,

Good addition!

John
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
ziolkoConnect With a Mentor Commented:
procedure TMainForm.Timer1Timer(Sender: TObject);
var tm: TTime;
begin
  Label1.Caption := FormatDateTime('hh:mm:ss', time);
  tm := EncodeTime(15, 0, 0, 0);
  if DateUtils.SameDateTime(DateTimePicker1.Time, dt) then
    ;

end;

ziolko.
0
 
ziolkoCommented:
... SameDateTime returns true if difference between times is less than milisecond

also you may try:

if Abs(DateTimePicker1.Time - dt) < 1000 then

then it will return true if time difference is within second

ziolko.
0
 
peterkiersAuthor Commented:
This below is what i got, rfwoolf is right it doesn't work :

procedure TMainForm.FormCreate(Sender: TObject);
begin
  timer1.Enabled := True;
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
  Label1.Caption := FormatDateTime('hh:mm:ss', time);
end;


procedure TMainForm.DateTimePicker1Change(Sender: TObject);
begin
AlarmDateTime := DateTimePicker1.Time
end;

procedure TMainForm.Timer2Timer(Sender: TObject);
begin
  if AlarmDateTime = Now then
   ShowMessage('Het is nu'+  FormatDateTime('hh:mm:ss', time));
end;

end.
0
 
rfwoolfCommented:
uses
dateutils;

procedure TMainForm.Timer2Timer(Sender: TObject);
begin
  if SecondsBetween(AlarmDateTime, Now) < 5 then
   begin
   ShowMessage('Het is nu'+  FormatDateTime('hh:mm:ss', time));
    Timer2.enabled := false;
   end;
end;
0
 
peterkiersAuthor Commented:
Thanks for the help only the time keeps running in the messagebox too
and that is not good, for example if its 15:00 then the message should
display 'it is now 15:00'.

Because i get so much help i increase the points so i can divide it and
assign the points to everyone.

P.

0
 
rfwoolfCommented:
There is a component from the JEDI VCL suite that features a system clock. You just place it on your form and it will show the system time.
I'm not sure I understand your last post...
0
 
peterkiersAuthor Commented:
i think in the ShowMessageLine is something wrong.
because when the showmessage appears the time
in the showmessage runs too.

another explenation:
if i set the DateTimepicker to 15:00 and if the systemtime
is 15:00 too, the showmessage will be displayed, but the
time in the showmessage runs too. And it only has to inform
the user that it is 15:00.

procedure TMainForm.Timer2Timer(Sender: TObject);
begin
  if SecondsBetween(AlarmDateTime, Now) < 5 then
   begin
   ShowMessage('Het is nu'+  FormatDateTime('hh:mm:ss', time));
    Timer2.enabled := false;
   end;
end;
0
 
rfwoolfCommented:
Okay, so you want it to instead display the time in the DateTimePicker?

I haven't tested this because I'm not in front of Delphi right now, but try something like this...

procedure TMainForm.Timer2Timer(Sender: TObject);
begin
  if SecondsBetween(AlarmDateTime, Now) < 5 then
   begin
   ShowMessage('Het is nu ' + TimeToStr(DateTimePicker.Time));
    Timer2.enabled := false;
   end;
end;
0
 
peterkiersAuthor Commented:
Thank you erverybody for all the help
greetings,
Peter Kiers
0
 
ziolkoCommented:
what the hell???
>>if Abs(DateTimePicker1.Time - dt) < 1000 then

I don't know what I've been thinking when I wrote this

p.s.
 dank u wel peter:)

ziolko.
0
 
JohnjcesCommented:
WOW, I just came back to this and great dialog!

Thanks!

Ziolko... I am here at work and sent off several official emails with August and April as being the current month and time frame... so maybe < 1000 isn't all that bad! :)

John
0
 
JohnjcesCommented:
Thanks! := thanks to peterkiers
0
All Courses

From novice to tech pro — start learning today.