[fpc-pascal] date formating dificulty
Michael Van Canneyt
michael at freepascal.org
Tue Nov 18 13:27:33 CET 2014
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.
More information about the fpc-pascal
mailing list