Conversion with StrToDateTime and TFormatSettings does not work

后端 未结 2 1876
旧巷少年郎
旧巷少年郎 2020-12-16 13:23

This code should work in Delphi XE2, but it gives \"not a valid date and time\" error in StrtoDateTime conversion:

procedure TForm2.Button1Click(Sender: TObj         


        
2条回答
  •  天命终不由人
    2020-12-16 14:16

    If you want to convert some special DateTime-Formats you should better use VarToDateTime instead of StrToDateTime. Just have a look at the implementation of both and you will recognize, that StrToDateTime is somehow ... and VarToDateTime will ask the OS if it can't determine by itself.

    This works with Delphi XE3 (but should also work with earlier versions):

    procedure TForm2.Button1Click( Sender: TObject );
    var
      s: string;
      d: TDateTime;
      FmtStngs: TFormatSettings;
    begin
        GetLocaleFormatSettings( GetThreadLocale, FmtStngs );
        FmtStngs.DateSeparator := #32;
        FmtStngs.ShortDateFormat := 'dd mmm yyyy';
        FmtStngs.TimeSeparator := ':';
        FmtStngs.LongTimeFormat := 'hh:nn';
    
        s := FormatDateTime( '', Now, FmtStngs );
        d := VarToDateTime( s );
    end;
    

提交回复
热议问题