11 Vote

Delphi/Lazarus: Determine System Date Format

Question by Guest | Last update on 2024-01-23 | Created on 2016-04-23

The function FormatDateTime() can be used for converting a TDateTime formatted date into a readable date. For that, you can use the corresponding constants or placeholders such as yyyy/mm/dd and so on.

However, I would like to write a program that can also be used in other countries. And depending on the country, another date formatting is used. For example, the USA is using the format dd/mm/yyyy, Germany is using the dd.mm.yyyy formatting and so on.

So, is there any possibility in Delphi or Lazarus to read out the appropriate format string of the computer on which the application is running?

ReplyPositiveNegative
2Best Answer2 Votes

Yes. There are the constants ShortDateFormat and ShortTimeFormat. Those variables are always holding the required system formatting.

You can simply use these constants with the function FormatDateTime, for example like that:

var
  s: string;
begin
  s := FormatDateTime(ShortDateFormat, now);
  ShowMessage('System-Formatted Date: ' + s); 

  s := FormatDateTime(ShortTimeFormat, now);
  ShowMessage('System-Formatted Time: ' + s); 

  s := ShortDateFormat + ' ' + ShortTimeFormat;
  s := FormatDateTime(s, now);
  ShowMessage('Date and Time: ' + s); 
end;

In this example, first we only output the date, after that only the time and lastly date and time together.
Last update on 2024-01-23 | Created on 2016-04-23

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

VirtualBox: Change Date and Time

Tutorial | 10 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.