在 C++Builder 2009 中将字符串转换为 TDateTime
Converting string to TDateTime in C++Builder 2009
我尝试使用 C++Builder 2009 将特定格式的字符串转换为 TDateTime
:
TDateTime dt, dt2;
TFormatSettings FS, FS2;
UnicodeString datestring = "17/10/2017 13:24:33";
UnicodeString datestring2 = "2017.17.10 13:24:33";
FS.DateSeparator = '/';
FS.ShortDateFormat = "dd/mm/yyyy";
FS.LongTimeFormat = "hh:nn:ss";
FS.TimeSeparator = ':';
FS2.DateSeparator = '.';
FS2.ShortDateFormat = "yyyy.dd.mm";
FS2.LongTimeFormat = "hh:nn:ss";
FS2.TimeSeparator = ':';
try{
dt = StrToDateTime(datestring, FS);
dt2 = StrToDateTime(datestring2,FS2);
}catch(EConvertError& e)
{
int a = 2;
}
转换dt
没问题,但转换dt2
会抛出异常:
''2017.17.10 13:24:33'' is not a valid date and time
根据 StrToDate()
的文档(也适用于 StrToDateTime()
):
S
must consist of two or three numbers, separated by the character defined by the DateSeparator
global variable or its TFormatSettings
equivalent. The order for month, day, and year is determined by the ShortDateFormat
global variable or its TFormatSettings
equivalent--possible combinations are m/d/y, d/m/y, and y/m/d.
失败的日期是 y/d/m
格式,这些 RTL 函数不支持这种格式。有效的日期是 d/m/y
格式,受支持。
谢谢大家!
好的,现在我知道了,StrToDateTime 不支持这种日期格式。这个问题的解决方案是,转换并合并 Windows ShortDateFormat 和 LongTimeFormat 以格式化 strptime() 从 time.h 接受的字符串。然后我使用 strptime() 并从 tm struct from time.h. I try to link docs but, in docs isn't any strptime func. I find this func in time.h from CodeGear RTL ver 13. I think this is equivalent to strptime
创建 TDateTime
我尝试使用 C++Builder 2009 将特定格式的字符串转换为 TDateTime
:
TDateTime dt, dt2;
TFormatSettings FS, FS2;
UnicodeString datestring = "17/10/2017 13:24:33";
UnicodeString datestring2 = "2017.17.10 13:24:33";
FS.DateSeparator = '/';
FS.ShortDateFormat = "dd/mm/yyyy";
FS.LongTimeFormat = "hh:nn:ss";
FS.TimeSeparator = ':';
FS2.DateSeparator = '.';
FS2.ShortDateFormat = "yyyy.dd.mm";
FS2.LongTimeFormat = "hh:nn:ss";
FS2.TimeSeparator = ':';
try{
dt = StrToDateTime(datestring, FS);
dt2 = StrToDateTime(datestring2,FS2);
}catch(EConvertError& e)
{
int a = 2;
}
转换dt
没问题,但转换dt2
会抛出异常:
''2017.17.10 13:24:33'' is not a valid date and time
根据 StrToDate()
的文档(也适用于 StrToDateTime()
):
S
must consist of two or three numbers, separated by the character defined by theDateSeparator
global variable or itsTFormatSettings
equivalent. The order for month, day, and year is determined by theShortDateFormat
global variable or itsTFormatSettings
equivalent--possible combinations are m/d/y, d/m/y, and y/m/d.
失败的日期是 y/d/m
格式,这些 RTL 函数不支持这种格式。有效的日期是 d/m/y
格式,受支持。
谢谢大家!
好的,现在我知道了,StrToDateTime 不支持这种日期格式。这个问题的解决方案是,转换并合并 Windows ShortDateFormat 和 LongTimeFormat 以格式化 strptime() 从 time.h 接受的字符串。然后我使用 strptime() 并从 tm struct from time.h. I try to link docs but, in docs isn't any strptime func. I find this func in time.h from CodeGear RTL ver 13. I think this is equivalent to strptime
创建 TDateTime