秦人再现 發表於 2020-3-7 14:52:00

Delphi 解决StrToDateTime()不是有效日期类型的问题

<p>相关资料:</p>
<p>http://www.delphitop.com/html/shijian/4924.html</p>
<div>方法<span class="hljs-number">1:</span></div>
<div><span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">GetDateFormat<span class="hljs-params">():<span class="hljs-keyword">string;</span></span></span></span></span></div>
<div><span class="hljs-keyword">var</span></div>
<div>&nbsp; SysFrset: TFormatSettings;</div>
<div><span class="hljs-keyword">begin</span></div>
<div>&nbsp; Result:=<span class="hljs-string">'';</span></div>
<div>&nbsp; GetLocaleFormatSettings(GetUserDefaultLCID, SysFrset);</div>
<div>&nbsp; Result:=SysFrset.DateSeparator;&nbsp; <span class="hljs-comment">//DateSeparator当前系统日期分隔符</span></div>
<div><span class="hljs-comment">end;</span></div>
<div>&nbsp;</div>
<div>
<div><span class="hljs-comment">procedure TForm1.btn1Click(Sender: TObject);</span></div>
<div><span class="hljs-comment">var&nbsp; &nbsp;</span></div>
<div><span class="hljs-comment">&nbsp; Dat:TDateTime;</span></div>
<div><span class="hljs-comment">&nbsp; Fr, Fm:string;//保存日期格式的字符串变量</span></div>
<div><span class="hljs-comment">&nbsp; str: string;</span></div>
<div><span class="hljs-comment">begin&nbsp;&nbsp;</span></div>
<div><span class="hljs-comment">&nbsp; Fr:=GetDateFormat;//获取当前系统日期分隔符</span></div>
<div><span class="hljs-comment">&nbsp; Fm:= Format('yyyy%sMM%sdd hh:mm:ss',);</span></div>
<div><span class="hljs-comment">&nbsp; str:= FormatDateTime(Fm,Now);</span></div>
<div><span class="hljs-comment">&nbsp; ShowMessage(str);</span></div>
<div><span class="hljs-comment">&nbsp; Dat:=StrToDateTime(str);&nbsp; //这样始终都会跟当前系统日期分隔符同步,这样不管你把当前系统日期分隔符设置为什么样都不会报错</span></div>
<div><span class="hljs-comment">end;</span></div>
<div>&nbsp;</div>
<div><span class="hljs-comment">方法2:</span></div>
<div>
<div><span class="hljs-comment">procedure TForm1.btn2Click(Sender: TObject);</span></div>
<div><span class="hljs-comment">var</span></div>
<div><span class="hljs-comment">&nbsp; D:TDateTime;</span></div>
<div><span class="hljs-comment">&nbsp; s:string;</span></div>
<div><span class="hljs-comment">begin</span></div>
<div><span class="hljs-comment">//&nbsp; D:=VarToDateTime('05-10-14 04:35PM');</span></div>
<div><span class="hljs-comment">&nbsp; D:=VarToDateTime('2018-03-13 12:12:12');</span></div>
<div><span class="hljs-comment">&nbsp; S:=FormatDatetime('YYYY/MM/DD HH:MM:SS',D);</span></div>
<div><span class="hljs-comment">&nbsp; showmessage(s);</span></div>
<div><span class="hljs-comment">end;</span></div>
<div>&nbsp;</div>
<div><span class="hljs-comment">----------------------------------</span></div>
<div><span class="hljs-comment">在&nbsp;DELPHI&nbsp;IDE&nbsp;下&nbsp;按下&nbsp;CTRL+SHIFT+F&nbsp;。搜索&nbsp;Application.Initialize;&nbsp;就能找到&nbsp;代码的修改位置。<br>另外&nbsp;也可以不要&nbsp;Application.Initialize;&nbsp;,然后代码放到&nbsp;其他位置。<br>特别是你第一个建立的&nbsp;对象的位置。<br>例如&nbsp;DataMoudle&nbsp;或者&nbsp;主界面,或者&nbsp;登录界面。<br>在他们的&nbsp;OnCreate&nbsp;里写就行了。<br>with&nbsp;FormatSettings&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShortDateFormat&nbsp;:=&nbsp;'yyyy-mm-dd';<br>&nbsp;&nbsp;&nbsp;&nbsp;LongDateFormat&nbsp;:=&nbsp;&nbsp;'yyyy-mm-dd';<br>&nbsp;&nbsp;&nbsp;&nbsp;ShortTimeFormat&nbsp;:=&nbsp;'hh:nn:ss';<br>&nbsp;&nbsp;&nbsp;&nbsp;LongTimeFormat&nbsp;:=&nbsp;'hh:nn:ss';<br>&nbsp;&nbsp;&nbsp;&nbsp;DateSeparator&nbsp;:=&nbsp;'-';<br>&nbsp;&nbsp;&nbsp;&nbsp;TimeSeparator&nbsp;:=&nbsp;':';<br>&nbsp;&nbsp;end;</span></div>
</div>
</div>

</div>
<div id="MySignature" role="contentinfo">
    <div id="AllanboltSignature">   
      <div>作者:疯狂Delphi</div>
      <div>出处:https://www.cnblogs.com/FKdelphi/</div>
      <div>本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.</div>
</div>
<div class="div_masklayer" id="div_masklayer"></div>
<div class="div_popup" id="Div_popup">
<p class="mid">欢迎关注我,一起进步!扫描下方二维码即可加我</p>
<img class="img_zfb" id="img_zfb" width="150" src="https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_251208113944_QQ.png">
<img class="img_zfb" id="img_zfb" width="150" src="https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_251208115029_WX.png">
</div><br><br>
来源:https://www.cnblogs.com/FKdelphi/p/12434249.html
頁: [1]
查看完整版本: Delphi 解决StrToDateTime()不是有效日期类型的问题