加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi字符串转日期,强大到窒息,VarToDateTime解决了困扰很久

发布时间:2020-12-15 09:52:56 所属栏目:大数据 来源:网络整理
导读: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 ; 尤其是在进行数据库语句操作时,对于字
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;

当然上述函数也可以简化为:
 
1 function SetFieldDate(str: string): Variant;
2 begin
3   if str = ‘‘ then
4     result := Null
5   else
6     result := VarToDateTime(str);
7 end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读