VarToDateTime(VarDateFromStr)使用哪种日期格式? [英] Which date format does VarToDateTime(VarDateFromStr) use?
问题描述
我最近在日期转换方面一直遇到问题.我的应用程序在某些工作站上运行,无法将字符串正确转换为日期.
I've been having problems lately with date conversion lately. Some workstations my application run on don't convert string to date correctly.
我将问题归结为VarDateFromStr,该问题似乎并没有检查LOCALE_SSHORTDATE进行转换.我想知道是否有人知道它对转换进行DID检查.还是不同的行为仅链接到不同的DLL版本?
I tracked the issue down to VarDateFromStr that doesn't seem to be checking LOCALE_SSHORTDATE to make the conversion. I was wondering if anyone knew what it DID check for the conversion. Or does the different behavior only linked to different DLL version?
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",即可解决此问题.不过,我仍然必须对此进行验证.
I've been told that changing the short date format (in the control panel) from 'dd-MM-yyyy' to whatever and back to 'dd-MM-yyyy' fixed the problem. I still have to verify this though.
Kindda忘了提,该问题仅在WinXP SP3上得到确认.
Kindda forgot to mention, the problem has only been confirmed on WinXP SP3 yet.
推荐答案
肯, VarToDateTime
函数在内部调用 VarDateFromStr
函数,该函数使用 VAR_LOCALE_USER_DEFAULT
常量来格式化日期.
Ken, the VarToDateTime
function internally calls the VarDateFromStr
function wich uses the VAR_LOCALE_USER_DEFAULT
constant to format the date.
要确定其中包含VAR_LOCALE_USER_DEFAULT
的格式,您可以使用此代码
to determine wich format contains the VAR_LOCALE_USER_DEFAULT
you can use this code
var
FormatSettings : TFormatSettings;
begin
GetLocaleFormatSettings(VAR_LOCALE_USER_DEFAULT, formatSettings);
ShowMessage('VarToDateTime is using this format to convert dates '+formatSettings.ShortDateFormat);
end;
现在为避免出现问题,您可以使用StrToDateTime函数将变量值转换为字符串,然后转换为日期时间
now to avoid your problem you can convert your variant value to string and then to datetime using the StrToDateTime function
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;
这篇关于VarToDateTime(VarDateFromStr)使用哪种日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!