Delphi has this function:
function VarToDateAsString(const V: TVarData): TDateTime;
....
LResult := VarDateFromStr(S, VAR_LOCALE_USER_DEFAULT, 0, Result);
doing this with today’s date:
VarDateFromStr('07;12;18', VAR_LOCALE_USER_DEFAULT, 0, Result);
then Result will be equal
Result = '07;18;12'
Is any fix possible ?
CODE EXAMPLE
procedure TForm1.Button1Click(Sender: TObject);
var
LDateTimeVar: Variant;
LDateTime: TDateTime;
begin
// Current date separator in OS settings is ';'
LDateTimeVar := '07;12;18';
LDateTime := VarToDateTime(LDateTimeVar);
// Expected LDateTime = '07;12;18',
// but will be LDateTime = '07;18;12'
ShowMessage(DateToStr(LDateTime));
end;
VarToDateAsStringcalls ‘oleaut32’sVarDateFromStrto perform the conversion, which simply fails if you’ve got the year in the middle.variants.VarToDateAsString:Put a breakpoint on the second line and you’ll see the
Resultparameter will reverse the day and the year. You can carry out tests yourself by callingactivex.VarDateFromStr.You can bypass
variants.VarToDateAsStringcallingVarDateFromStrand implement your own function, but don’t think using something fromsysutilsbecause it doesn’t support a year-in the middle format too.sysutils.TryStrToDateTimecallsScanDatewhich in turn callsGetDateOrder. The below is the entire function:As you can see there’s no outcome ordering with year in the middle.
Looks like you have to parse the string yourself: