Inverse function of FormatDateTime

后端 未结 7 619
自闭症患者
自闭症患者 2021-02-07 04:38

I am looking for a function to reverse any string (YYYYMDD,YY/MM/DD,YYMMDD,...) created by the function FormatDateTime to

7条回答
  •  醉梦人生
    2021-02-07 05:26

    Without using any external library, you can do something like:

    function AnyStringToDate(fmt, dt: String) : TDateTime;
    var
      fs : TFormatSettings;
    Begin
      fs := TFormatSettings.Create;
      fs.ShortDateFormat := fmt;
    
      result := StrToDateDef(dt, 0, fs);
    End;
    

    and then use it like:

      mydate := AnyStringToDate('YYYY-MM-DD', '2015-01-20');
    

    I havent compiled this, but the idea simple.

提交回复
热议问题