Recent

Author Topic: Show running time and date in application  (Read 15257 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
Show running time and date in application
« on: July 27, 2011, 08:28:37 pm »
Dear forum people,

I would like to show Today's date and current time continuously in the application. Also, should be able to cut and paste those values.

I find caendar, and read about the functions date and time, but they are not running data.

I was hoping there was a component already?

Thanks!

w click

  • Full Member
  • ***
  • Posts: 185
Re: Show running time and date in application
« Reply #1 on: July 27, 2011, 08:43:18 pm »
Try:-
Quote
label1.caption:=formatdatetime('ddd dd-mmm-yyyy hh:nn:ss.zzz');
Which will give you "Wed 27-Jul-2011 19:39:20.385".  Careful that "m" stands for month and "n" stands for minutes.  Milliseconds is probably over the top too.

As for cutting and pasting, you could go the route of having a button to paste into the clipboard, or just stick it in an edit1.text.

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: Show running time and date in application
« Reply #2 on: July 27, 2011, 08:49:48 pm »
@w click:
I was just about to post almost the same code - you beat me by seconds <grin>

I was also going to suggest putting this in a TTimer.OnTimer event so that it will constantly update, like so:

Code: [Select]
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Edit1.Text := FormatDateTime('DDDD, dd MMMM YYYY HH:MM:SS', Now);
end;

Ensure to set the .Interval value to 1000 if you want just seconds or 1 if you want to update milliseconds.

  regards,
      geno

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Show running time and date in application
« Reply #3 on: July 27, 2011, 09:00:27 pm »
Thanks Flash and Click,

Would this yield a continuously running clock/calendar that updates every second and every day, like a wall clock?

Or when something is clicked then the date and hour show in a static mode?

Thanks!

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: Show running time and date in application
« Reply #4 on: July 27, 2011, 09:47:12 pm »
Quote
Would this yield a continuously running clock/calendar that updates every second and every day, like a wall clock?

If you use it within a TTimer.OnTimer event, yes, it will update every second (if you set the interval to 1000). 

  geno.

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Show running time and date in application
« Reply #5 on: July 27, 2011, 11:39:48 pm »
Thank you, Fleshmang,

And so I don't misguide myself, will the TTimer ALSO update the date if 12 PM happens to come by, or does the TTimer only updates the hour:::?

Thanks!

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: Show running time and date in application
« Reply #6 on: July 28, 2011, 03:42:16 am »
TTimer is not doing anything but updating the text in the control.  The workhorse is the Now() function, it returns the current time and date as of the moment it is called; the timer calls it every second.   http://www.freepascal.org/docs-html/rtl/sysutils/now.html  
So yes, it will update the date at 12 AM - but take a look at the FormatDateTime function http://www.freepascal.org/docs-html/rtl/sysutils/formatdatetime.html  This determines what the output will look like - you can have it include AM/PM or use  1:00am/1300hrs - it takes a little tweaking to get the display the way you want it.

regards,
  geno.


Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Show running time and date in application
« Reply #7 on: July 28, 2011, 06:24:20 am »
Great Fleshmang,

I rather ask where I am going if I take an unknown route, and your answer is very clear, so I'll take the aproach as suggested. 8)

w click

  • Full Member
  • ***
  • Posts: 185
Re: Show running time and date in application
« Reply #8 on: July 28, 2011, 10:20:19 am »
Quote
I was just about to post almost the same code - you beat me by seconds <grin>
@Fleshmang
Aye, but you got all the credit ;-)

I was just pleased someone had finally asked the question that I know the answer to.

I found that I wanted a tenth of a second output, so:-

Quote
var s : string;

s:=formatdatetime('hh:nn:ss.zzz',now);
s:=copy(s,1,pos('.',s)+1);
label1.caption:=s;

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Re: Show running time and date in application
« Reply #9 on: July 28, 2011, 02:15:16 pm »
I'm using slight different implementation, without formatdatetime: (old and a bit slower code, but maybe can help someone)

Timer.Interval := 500;
Code: [Select]
    adesso := Time;
    DecodeTime(adesso, ora, minuti, secondi, msecondi);

    if FTimeTick = True then
    begin
      tick_sep:= '.';
      FTimeTick:= False;
    end
    else
    begin
      tick_sep:= ' ';
      FTimeTick:= True;
    end;

    s_ora := IntToStr(ora);

    s_minuti := IntToStr(minuti);
    if minuti < 10 then
      s_minuti := '0' + s_minuti;

    s_secondi := IntToStr(secondi);
    if secondi < 10 then
      s_secondi := '0' + s_secondi;

    StBr_MainStatusBar.Panels.Items[2].Text := s_ora + tick_sep + s_minuti + tick_sep + s_secondi;

or this with formatdatetime approach
Code: [Select]
    if FTimeTick = True then
    begin
      tick_sep:= '.';
      FTimeTick:= False;
    end
    else
    begin
      tick_sep:= ' ';
      FTimeTick:= True;
    end;

    format_date := 'hh' + tick_sep + 'nn' + tick_sep + 'ss';
    StBr_MainStatusBar.Panels.Items[2].Text := FormatDateTime(format_date, Now);

after looking on Delphi Basics I came out with this ultimate optimization, con you do better?
Code: [Select]
    if FTimeTick = True then
      TimeSeparator := '.'
    else
      TimeSeparator := ' ';

    FTimeTick := not FTimeTick;

    StBr_MainStatusBar.Panels.Items[2].Text := FormatDateTime('tt', Now);
« Last Edit: July 28, 2011, 02:42:11 pm by Shebuka »

 

TinyPortal © 2005-2018