procedure THRForm.Button1Click(Sender: TObject); var D:TDateTime; s:string; begin D:=VarToDateTime('05-10-14 04:35PM'); S:=FormatDatetime('YYYY-MM-DD HH:MM:SS',D); showmessage(s); end;
尤爲是在進行數據庫語句操做時,對於字符串的來源不肯定因素太多,有了該函數用起來真的很方便。舉例以下:數據庫
用VarToDateTime構建一個新的函數SetFieldDate,而後使用該函數爲數據庫時間字段賦值,只需函數
FQuery.Parameters.ParamValues['Brithday'] := SetFieldDate(edit1.Text);
function SetFieldDate(str: string): Variant; begin if str = '' then result := Null else result := StrToDateTime(FormatDatetime('YYYY-MM-DD', VarToDateTime(str))); end;
固然上述函數也能夠簡化爲:spa
1 function SetFieldDate(str: string): Variant; 2 begin 3 if str = '' then 4 result := Null 5 else 6 result := VarToDateTime(str); 7 end;