How do I convert an ISO 8601 string to a Delphi TDate?

前端 未结 7 1503
北海茫月
北海茫月 2020-12-06 05:09

I can convert a Delphi TDate to ISO 8601 format easily using this:

DateTimeToString(result, \'yyyy-mm-dd\', myDate);

What\'s the idiomatic

7条回答
  •  天命终不由人
    2020-12-06 05:33

    I think this should work... the documentation says the overloaded version of these methods is for use in threads, but it can be handy for specifying the format settings you wish to use at the time.

    Function ISO8601ToDateTime(Value: String):TDateTime;
    var
        FormatSettings: TFormatSettings;
    begin
        GetLocaleFormatSettings(GetThreadLocale, FormatSettings);
        FormatSettings.DateSeparator := '-';
        FormatSettings.ShortDateFormat := 'yyyy-MM-dd';
        Result := StrToDate(Value, FormatSettings);
    end;
    

    You can of course write variants of this with StrToDateDef and TryStrToDate with equivalent functionality

提交回复
热议问题