开发者

Which date format does VarToDateTime(VarDateFromStr) use?

开发者 https://www.devze.com 2023-01-22 17:37 出处:网络
I\'ve been having problems lately with date conversion lately. Some workstations my application run on don\'t convert string to date correctly.

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

I track开发者_StackOverflow中文版ed 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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消