[fpc-pascal] FormatDatetime is not respecting the specified pattern

Michael Van Canneyt michael at freepascal.org
Wed Feb 1 08:27:04 CET 2012



On Wed, 1 Feb 2012, Luciano de Souza wrote:

>     Hello listers,
> 
>  A strange error came up when formating a date. See this code:
> writeln(formatdatetime('dd/mm/YYYY', now));
> The answer should be: 31/01/2012
> The answer was: 31-01-2012
> I did one test else:
> writeln(formatdatetime('dd$mm$YYYY', now));
> The answer was: 31$01$2012
> Yes, the problem seems to be related to the backslash.
> DefaultFormatSettings.DateSeparator := '/';
> writeln(formatdatetime('dd/mm/YYYY', now));
> The answer was: 31/01/2012
> Right! That's the answer. But it was necessary to setup the settings manually.
> The test was done with Freepascal 2.4.4 and Ubuntu 10.10.
> Does someone know what is my mistake?

There is no mistake, all is as it should be.

In the format string for FormatDateTime, a / means 'use the date separator of the RTL'.

By default, on linux the RTL date separator is the '-' character.

If you want to initialize the RTL locale settings with the system settings,
just put 'clocale' as one of the first units in your uses clause. 
It will set all necessary constants.

if you want an actual / character in your output, you should do a

writeln(formatdatetime('dd"/"mm"/"YYYY', now));

i.e. enclose the special character in double quotes.

Michael.


More information about the fpc-pascal mailing list