57 Votes

Delphi/Lazarus: Display current Date and Time

Tip by Stefan Trost | Last update on 2023-06-03 | Created on 2014-08-07

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

For this, only two functions are important: With the function "now", we get the current time as a timestamp of the type TDateTime and with FormatDateTime we can convert this time stamp into a string that contains the date and/or the time in any freely controllable representation or formatting. We can then process this string further and output it anywhere in our program.

Let's first look at a small code as an example that outputs the current date and time as a message via ShowMessage:

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 to be displayed in the format TDateTime. Here, as time, we can use the function "now" directly, which can in this way pass its return value directly to the function FormatDateTime.

The Format String

Via the format string, we can specify our desired format for the date and the time. In this string, certain letters stand for the individual components of the date and the time, so that we can flexibly adjust the output.

For example, in the format string, "yyyy" represents a four-digit year, "yy" represents a two-digit year, "mm" represents a two-digit month and "dd", "hh", "nn" and "ss" accordingly represent day, hour, minute and second with two digits each and (as required) leading zeros if the respective values are less than 10. If we only use one of the two letters for month, day, hour, minute or second, the respective number will be written without leading zeros instead.

In our example above, we use the format string "yyyy-mm-dd, hh:nn:ss". That means, we start with the date with hyphens between year, month and day. The date is followed by the time with a colon between hours, minutes and seconds separated by a comma and a space. We'll look at more examples of other format strings in the next section.

Other common Formats

Other formatting of the date and the time is possible by adapting the format string according to your own needs:

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

As we can see in the examples, we can change the order of the individual components as we wish or even omit them, we can write the numbers without leading zeros or just output the date or the time alone.

Meaningful Letters in the Format String

Also letters can be used within the format string. But we have to be careful if we want to use letters that also stand for a time component. If you don't want to convert characters like "d" or "y" to their time equivalents and instead keep those letters as such, you have to put them in double quotes ("):

FormatDateTime('"day "d', now);             // day 31

Here we want to output the word "day" along with the number of the day. So that the "d" and the "y" in "day" are written as such, we put "day " into quotation marks.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Determine Week Number

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.