[fpc-pascal] Why has StrToDate() failing under FPC 2.3.1 (64bit)

Michael Van Canneyt michael at freepascal.org
Tue Oct 20 13:20:06 CEST 2009



On Tue, 20 Oct 2009, Graeme Geldenhuys wrote:

> On 20/10/2009, Graeme Geldenhuys <graemeg.lists at gmail.com> wrote:
>>  PS:
>>  Oh and a nice hidden little features (I couldn't find documentation
>>  for). When you specify a date format as 'yyyy/mm/dd' or dd/mm/yy
>>  etc... The '/' character is NOT the separator!
>
>
> Correction, that applies to FormateDateTime(), not StrToDate(). I got
> a bit confused.
>
> Here is an example of what I meant:
>
> -----------------------------
> procedure TestDate(S: string);
> begin
> writeln(s + ' formatting  => ' + FormatDateTime(s, Date));
> end;
>
> begin
> DateSeparator := '?';  // force separator to ? character
>
> Writeln('DateSeparator = ', DateSeparator);
> Writeln('-------');
> TestDate('yyyy-mm-dd');
> TestDate('yyyy~mm~dd');
> TestDate('yyyy/mm/dd');
> TestDate('yyyy''/''mm''/''dd');
> end.
>
> -----------------------------
>
> And here follows the output:
>
>
> c:\temp>project1
> DateSeparator = ?
> -------
> yyyy-mm-dd formatting  => 2009-10-20
> yyyy~mm~dd formatting  => 2009~10~20
> yyyy/mm/dd formatting  => 2009?10?20
> yyyy'/'mm'/'dd formatting  => 2009/10/20
>
>
> As you can see. If you want to force '/' as your date delimiter,
> irrespective to what DateSeparator is set at, you have to place the
> the '/' in qoutes so it doesn't get interpreted as a place-holder for
> DateSeparator. Sneaky code!

But perfectly documented...

I agree that it is tricky. I often had to correct code from people who
didn't understand this and inserted some kind of formatdatetime('mm/dd/yyy')
in an SQL statement and were surprised to see '04 09 2005' in their SQL,
which of course generated an error.

Michael.



More information about the fpc-pascal mailing list