How to use Tformatsettings in Delphi 10 Tokyo?

Asked

Viewed 1,130 times

0

How do I set regional date, time, decimal separator, mile separator settings in Delphi 10 Tokyo? Previously, in Delphi 7, there were global system variables. In newer versions they created the record TFormatSettings, but apparently there’s no effect on the changes I’m making.

Example: I am running my program on a PC where the local date and time are in American format, i.e., 'MM/DD/YYYY h:mm:ss AM/PM' (inverted month and 12 hours + AM/PM). I want my application to be in the Brazilian standard, i.e., "DD/MM/YYYY HH:mm:ss" (24 hours).

How to apply this change to my local settings using TFormatSettings?

1 answer

3


This is an example of how I use

procedure FillFormatSettings(var FSettings: TFormatSettings);
begin
{$IFNDEF VER180}
FSettings:=TFormatSettings.Create('pt-BR');
{$ENDIF}
FSettings.DateSeparator:=#47;
FSettings.ShortDateFormat:='dd/mm/yyyy';
FSettings.TimeSeparator:=#58;
FSettings.LongTimeFormat:='hh:nn:ss';
FSettings.ShortTimeFormat:='hh:nn';
end;

function DateTimeToStrBrazilian(DateTime: TDateTime): String;
var
  FmtSettings: TFormatSettings;
begin
FillFormatSettings(FmtSettings);
Result:=DateToStr(DateTime, FmtSettings)+' '+TimeToStr(DateTime, FmtSettings)
end;

In new versions of Delphi you need to instantiate a variable of type TFormatSettings, configure it and then use it in the method you want as for example in DateToStr(Data, VariavelFormatSettings)

EDITED

To change the global setting you can use the global Delphi variable FormatSettings which belongs to the System.SysUtils.

Example:

FormatSettings.DecimalSeparator:=#47;
FormatSettings.ShortDateFormat:='dd/mm/yyyy';
  • Thank you Matheus. However, in older versions of Delphi, once set the global settings it was no longer necessary to pass my custom formatting to anything, as you showed in your DateToStr(Data, VariavelFormatSettings). This is no longer possible in current versions of Delphi?

  • 1

    Of course, it is possible yes, just use the global variable of Delphi FormatSettings she belongs to System.SysUtils. So you could do more or less like this, for example,FormatSettings.DateSeparator

  • 1

    You’re really right. I was trying to create the variable and just set the record TFormatSettings directly without creating anything. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.