Which date format does VarToDateTime(VarDateFromStr) use?

。_饼干妹妹 提交于 2019-12-23 03:52:35

问题


I've been having problems lately with date conversion lately. Some workstations my application run on don't convert string to date correctly.

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'

EDIT: 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.

EDIT2: Kindda forgot to mention, the problem has only been confirmed on WinXP SP3 yet.


回答1:


Ken, the VarToDateTime function internally calls the VarDateFromStr function wich uses the VAR_LOCALE_USER_DEFAULT constant to format the date.

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;

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;


来源:https://stackoverflow.com/questions/4070105/which-date-format-does-vartodatetimevardatefromstr-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!