'23 / 02/2011 12:34:56'是无效的日期和时间 [英] '23/02/2011 12:34:56' is not valid date and time
问题描述
在我的代码中,我面临一个问题。示例代码:
In my code I am facing a problem. Example code:
var
d1: tdatetime
begin
d1 := strtodatetime('23/02/2011 12:34:56');
end;
但它出现错误:
'23 / 02/2011 12:34:56'无效
日期和时间
'23/02/2011 12:34:56' is not valid date and time
我正在做什么?
推荐答案
StrToDateTime 函数使用 ShortDateFormat
和 DateSeparator
转换日期部分和 LongTimeFormat
和 TimeSeparator
到时间部分。所以你的字符串必须与这些变量匹配才能将字符串转换为TDateTime。相反,您可以使用StrToDateTime与 TFormatSettings
参数,解析你的字符串。
the StrToDateTime function uses the ShortDateFormat
and DateSeparator
to convert the date part and the LongTimeFormat
and TimeSeparator
to the time part. so you string must match with theses variables to convert the string to TDateTime. instead you can use the StrToDateTime with the TFormatSettings
parameter, to parse you string.
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);
这篇关于'23 / 02/2011 12:34:56'是无效的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!