Delphi StrToDate 不接受日期
Delphi StrToDate not accepting date
我正在尝试使用 StrToDate 方法将日期从字符串转换为 TDate,但是当我以 yyyy/mm/dd 格式将日期传递给它时,它给我这个错误:'''2013/11 /12'' 不是有效日期'。任何想法我做错了什么?谢谢
var
newDate: TDate;
begin
newDate := StrToDate(sDate);
end;
仅将 string
作为输入的 StrToDate()
的重载版本使用来自 OS 的用户默认区域设置。该错误表示字符串与用户的日期区域设置格式不匹配。
使用接受 TFormatSettings
作为输入的 StrToDate()
的重载版本,这样您就可以指定所需的格式:
var
newDate: TDate;
fmt: TFormatSettings;
begin
// TFormatSettings.Create() was added in XE
// and GetLocaleFormatSettings() was deprecated
//
// fmt := TFormatSettings.Create;
GetLocaleFormatSettings(0, fmt);
fmt.ShortDateFormat := 'yyyy/mm/dd';
fmt.DateSeparator := '/';
newDate := StrToDate(sDate, fmt);
end;
我正在尝试使用 StrToDate 方法将日期从字符串转换为 TDate,但是当我以 yyyy/mm/dd 格式将日期传递给它时,它给我这个错误:'''2013/11 /12'' 不是有效日期'。任何想法我做错了什么?谢谢
var
newDate: TDate;
begin
newDate := StrToDate(sDate);
end;
仅将 string
作为输入的 StrToDate()
的重载版本使用来自 OS 的用户默认区域设置。该错误表示字符串与用户的日期区域设置格式不匹配。
使用接受 TFormatSettings
作为输入的 StrToDate()
的重载版本,这样您就可以指定所需的格式:
var
newDate: TDate;
fmt: TFormatSettings;
begin
// TFormatSettings.Create() was added in XE
// and GetLocaleFormatSettings() was deprecated
//
// fmt := TFormatSettings.Create;
GetLocaleFormatSettings(0, fmt);
fmt.ShortDateFormat := 'yyyy/mm/dd';
fmt.DateSeparator := '/';
newDate := StrToDate(sDate, fmt);
end;