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

delphi – ’23 / 02/2011 12:34:56’是无效的日期和时间

发布时间:2020-12-15 10:09:20 所属栏目:大数据 来源:网络整理
导读:在我的代码中,我面临一个问题.示例代码: var d1: tdatetimebegin d1 := strtodatetime('23/02/2011 12:34:56');end; 但它给出错误: ’23/02/2011 12:34:56′ is not valid date and time 我在做什么有什么问题? 解决方法 StrToDateTime功能使用 ShortDate
在我的代码中,我面临一个问题.示例代码:
var 
    d1: tdatetime
begin
    d1 := strtodatetime('23/02/2011 12:34:56');
end;

但它给出错误:

’23/02/2011 12:34:56′ is not valid
date and time

我在做什么有什么问题?

解决方法

StrToDateTime功能使用 ShortDateFormatDateSeparator将日期部分,LongTimeFormatTimeSeparator转换为时间部分.所以你的字符串必须与这些变量匹配才能将字符串转换为TDateTime.相反,您可以使用带有 TFormatSettings参数的StrToDateTime来解析字符串.
function StrToDateTime(const S: string; const FormatSettings: TFormatSettings): TDateTime;

检查这个样本

Var
StrDate : string;
Fmt     : TFormatSettings;
dt      : TDateTime;
begin
fmt.ShortDateFormat:='dd/mm/yyyy';
fmt.DateSeparator  :='/';
fmt.LongTimeFormat :='hh:nn:ss';
fmt.TimeSeparator  :=':';
StrDate:='23/02/2011 12:34:56';
dt:=StrToDateTime(StrDate,Fmt);

(编辑:李大同)

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

    推荐文章
      热点阅读