13 Votes

Delphi/Lazarus: Display current Date and Time

Tip by Delphian | Last update on 2020-10-20 | Created on 2014-08-07

Today, I would like to show you how to be able to show the date and/or time in your Delphi or Lazarus application.

For this, only two functions are important. With the function "now", you can get the current timestamp as TDateTime and using FormatDateTime, you can change this into an arbitrary formatting. The result is a string, you can process further.

var
  s: string;
begin
  s := FormatDateTime('yyyy-mm-dd, hh:nn:ss', now);
  ShowMessage(s); 
end;

FormatDateTime requires two parameters: a format string and the time that should be displayed (here: "now").

In the format string, "yyyy" stands for a year in 4 digits, "mm" is a 2 digit months and accordingly, "dd", "hh", "nn" and "ss" are standing for day, hour, minute and second.

Other formats are possible by changing the formatting string:

FormatDateTime('dd/mm/yyyy hh:nn:ss', now);  // 31/12/2020 23:59:00
FormatDateTime('dd.mm.yyyy, hh:nn:ss', now); // 31.12.2020, 23:59:00
FormatDateTime('dd/mm/yy', now);             // 31/12/99
FormatDateTime('yy/mm/dd', now);             // 99/12/31
FormatDateTime('yyyy/mm/dd', now);           // 2020/12/31
FormatDateTime('yy-mm-dd-hh-nn-ss', now);    // 99-12-31-23-59-00
FormatDateTime('d.m.yyyy', now);             // 1.1.2000

If you would like to keep characters such as "d" or "y", you have to put them into double quotes (").

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.