[Lazarus] RE : StrToDateTime question

Ludo Brands ludo.brands at free.fr
Sun May 27 08:51:51 CEST 2012


> How do I convert a string in the format
> yyyy-mm-dd to a DateTime variable?
> 
> StrToDateTime('2012-05-26', ????); // what goes there?//
> 

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.

Note that specifying a ShortDateFormat of 'yyyy-mm-dd' is not enough to set
the DateSeparator to '-'. 

Ludo





More information about the Lazarus mailing list