[fpc-pascal] date formating dificulty

Michael Van Canneyt michael at freepascal.org
Wed Nov 19 21:04:15 CET 2014

On Wed, 19 Nov 2014, Philippe wrote:

> Em 19.11.2014 12:17, Philippe escreveu:
>       Em 18.11.2014 10:27, Michael Van Canneyt escreveu:
> On Tue, 18 Nov 2014, Philippe wrote:
>                   Em 18.11.2014 09:49, Philippe escreveu: I wrote a program: - windows 8 32 bits - lazarus 1.2.4  ... fpc 2.6.4 then I copied it to another
>                   computer - windows 7 64 bits - lazarus 1.0.14 .... fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what
>                   was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find
>                   anything :( !) why formatDateTime( 'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ?   strtodate( formatDateTime( 'DD/MM/YYYY',
>                   now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits.   I am confused with the following syntax function
>                   FormatDateTime(   const FormatStr: string;   DateTime: TDateTime;   const FormatSettings: TFormatSettings ):string; how does it work
>                   between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to
>                   avoid bad surprise moving program from an OS to another ! Philippe   on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html I
>                   found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln
>                   (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date));
>                   TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that
>                   ShortDateFormat is deprecated ... which is not informed in the doc ...
> It should be, I will check.
> Anyway, as Graeme hinted: ShortDateFormat contains a format string, and / is a placeholder for the date 
> separator character on the computer on which the program is running.
> If you want to force a character, you can enclose it in quotes:
> ShortDateFormat:='dd"/"mm"/"yyyy'
> will always use / as the date separator, no matter what the internatonalization settings on the PC.
> But it is bad practice to do so.
> Secondly, it is a common mistake to think that StrToDate will always correctly perform the opposite of DateToStr.
> It does not, but this is documented.
> Michael.
> I still don't know how to use at the same time formatStr and FormatSettings in FormatDateTime.
> I´ll like very much to find a tutorial or some example about the good practice to manage dates and be sure it will do the same thing on different machines ....

Good practice is to avoid converting to and from string the whole time.
If you need to store a timestamp, use the ISO 8601 format or RFC 3339 (or is it 3999?).

Regarding formatsettings:

Since some versions, most date/time/money/float formatting routines support passing a TFormatSettings record to it to control the formatting.
(this is mostly done for multi-threaded apps where different threads can have different settings, like in web-apps).

If no TFormatSettings record is passed, the global FormatSettings record is used.

The various format variables which existed prior to this change, 
have simply been moved to this global FormatSettings record, with some backwards compatibility code.

So if you had old code that did something like:


you should now use


That is all there is to it. Hardly worth a tutorial.

But if you have suggestions for the documentation, I will be glad to add them.


More information about the fpc-pascal mailing list