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

前端 未结 7 1379
北海茫月
北海茫月 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:34

    why re-invent the wheel?

    XML uses ISO 8601 for date and date-time storage.

    Delphi has had built-in support for that since Delphi 6 in the XSBuiltIns unit.

    This answer explains how for DateTime, this is for Date only using the TXSDate class:

    with TXSDate.Create() do
      try
        AsDate := Date; // convert from TDateTime
        DateString := NativeToXS; // convert to WideString
      finally
        Free;
      end;
    
    with TXSDate.Create() do
      try
        XSToNative(DateString); // convert from WideString
        Date := AsDate; // convert to TDateTime
      finally
        Free;
      end;
    

提交回复
热议问题