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