0

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.

elcharlie
  • 511
  • 8
  • 25
  • possible duplicate of [Conversion with StrToDateTime and TFormatSettings does not work](http://stackoverflow.com/questions/13308472/conversion-with-strtodatetime-and-tformatsettings-does-not-work) – Sir Rufo Mar 31 '15 at 09:57
  • 2
    You have a typo in your code. Use `FS.TimeSeparator := ':';` – TLama Mar 31 '15 at 10:04

1 Answers1

1

You are configuring DateSeparator twice

  FS.DateSeparator := '-';
  FS.TimeSeparator := ':';
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159