Search code examples
delphidelphi-xe7tdatetime

Error converting string to datetime in Delphi FireMonkey XE7


I have a problem when converting a string to TDateTime in FireMonkey , in mobile devices.

It always gives me error and do not know how to do.

I show you what I do:

function StringToDateTime(DateStr: String): TDateTime;
var
  FS: TFormatSettings;
begin
  result := now;
  FS:= TFormatSettings.Create;
  FS.DateSeparator := '-';
  FS.DateSeparator := ':';
  FS.ShortDateFormat := 'dd-mm-yyyy';
  FS.ShortTimeFormat := 'hh:nn:ss';
  try
   Result := StrToDateTime(DateStr, FS); //the format of the string is : 
         // dd-mm-yyyy hh:nn:ss  '31-03-2015 9:36:00'
  except on E: Exception do
   ShowMessage(e.ToString);
  end;

end;

The exception is giving:

'31-03-2015 9:36:00' is not a valid date and time.


Solution

  • You are configuring DateSeparator twice

      FS.DateSeparator := '-';
      FS.TimeSeparator := ':';