0
votes

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.

1
You have a typo in your code. Use FS.TimeSeparator := ':'; - TLama

1 Answers

1
votes

You are configuring DateSeparator twice

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