VarToDateTime(VarDateFromStr)使用哪种日期格式?

时间:2010-11-01 15:07:26

标签: windows delphi ole

我最近在日期转换方面遇到了问题。我的应用程序运行的一些工作站没有正确地将字符串转换为日期。

我将问题跟踪到VarDateFromStr,似乎没有检查LOCALE_SSHORTDATE来进行转换。我想知道是否有人知道DID检查转换的内容。或者不同的行为只链接到不同的DLL版本?

GetLocaleStr(GetThreadLocale, LOCALE_SSHORTDATE, 'm/d/yy'); // returns 'dd-MM-yyyy'
FormatDateTime('dd-MM-yyyy', VarToDateTime('05-11-2010')); //returns '11-05-2010'

编辑: 我被告知将短日期格式(在控制面板中)从'dd-MM-yyyy'更改为无论并返回'dd-MM-yyyy'修复了问题。我仍然需要验证这一点。

EDIT2:Kindda忘记提及,问题仅在WinXP SP3上得到确认。

1 个答案:

答案 0 :(得分:3)

Ken,VarToDateTime函数在内部调用VarDateFromStr函数,该函数使用VAR_LOCALE_USER_DEFAULT常量来格式化日期。

确定包含VAR_LOCALE_USER_DEFAULT的格式,您可以使用此代码

var
 FormatSettings      : TFormatSettings;
begin
      GetLocaleFormatSettings(VAR_LOCALE_USER_DEFAULT, formatSettings);
      ShowMessage('VarToDateTime is using this format to convert dates  '+formatSettings.ShortDateFormat);
end;

现在为了避免您的问题,您可以使用StrToDateTime函数将变量值转换为字符串然后转换为datetime

var
v                   : variant;
FormatSettings      : TFormatSettings;
Begin
      v:='05-11-2010';//this is your variant.
      FormatSettings.ShortDateFormat:='dd-mm-yyyy';//use this format in the conversion
      ShowMessage(FormatDateTime('dd-MM-yyyy', StrToDateTime(V,FormatSettings)));
end;