[Lazarus] RE : StrToDateTime question

Marco van de Voort marcov at stack.nl
Tue May 29 15:23:34 CEST 2012


On Sun, May 27, 2012 at 08:51:51AM +0200, Ludo Brands wrote:
> The second parameter is only needed when '2012-05-26' is not in your locale
> date format. The way you use it is:
> 
> var
>   FS:TFormatSettings;
>   dt:tdatetime;
> begin
>   FS:=DefaultFormatSettings;
>   FS.DateSeparator:='-';
>   FS.ShortDateFormat:='yyyy-mm-dd';
>   dt:=StrToDateTime('2012-05-26',FS);
> end.

Or use the more general case, scandatetime.
 
http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

which is essentially the inverse of formatdatetime (as far as reasonably
possible.


there are some more comments about what it can and cannot do in the source:

{
  Inverse of formatdatetime, destined for the dateutils unit of FPC.

  Limitations/implementation details:
  - An inverse of FormatDateTime is not 100% an inverse, simply because one
  - can put e.g. time tokens twice in the format string,
       and scandatetime wouldn't know which time to pick.
  - Strings like hn can't be reversed safely. E.g. 1:2 (2 minutes after 1)
  - delivers 12 which is parsed as 12:00 and then
       misses chars for the "n" part.
  - trailing characters are ignored.
  - no support for Eastern Asian formatting characters since they are
  - windows only.
  - no MBCS support.

  Extensions
  - #9 eats whitespace.
  - whitespace at the end of a pattern is optional.
  - ? matches any char.
  - Quote the above chars to really match the char.
}





More information about the Lazarus mailing list