在FireMonkey中,在移動設備中將字符串轉換爲TDateTime時出現問題。將字符串轉換爲日期時間在Delphi中的錯誤FireMonkey XE7
它總是給我錯誤,不知道該怎麼做。
我告訴你我做什麼:
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;
唯一的例外是給:
'31 -03-2015 9時36分00' 秒不是一個有效的日期和時間。
可能重複http://stackoverflow.com/questions/13308472/conversion-with-strtodatetime-and -tformatsettings-does-not-work) – 2015-03-31 09:57:41
你的代碼中有一個錯字。使用'FS.TimeSeparator:=':';' – TLama 2015-03-31 10:04:35