Inverse function of FormatDateTime

后端 未结 7 621
自闭症患者
自闭症患者 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:00

    I know its too late but just for interest sake, with Delphi XE6 onwards you can now do the following

    uses
      System.DateUtils;
    var
      dt: TDateTime;
    begin
      dt := ISO8601ToDate('20190408');
    end.
    

    dt will now be 2019/04/08

    0 讨论(0)
  • 2021-02-07 05:01

    Did you check StrToDate and StrToDateTime ?

    0 讨论(0)
  • 2021-02-07 05:04

    It is quite easy with existing solution, StrToDateFmt function in rxDateutil.pas unit from RX package, which can be downloaded here: http://sourceforge.net/projects/rxlib/

    EDIT:

    Mentioned above function and StrToDateFmt from rxDateutil.pas are doing exactly what you expect, converting string to datetime using specified string mask, the code is too large to be included as this unit contains also other date functions, some of them required for converting string to date.

    Example of use:

    Result := StrToDateFmtDef('MM/DD/YYYY', '11/11/2011', Now);
    
    0 讨论(0)
  • 2021-02-07 05:09
    Function InvFormatDatetime (Cadena:String; Formato:String) : TDateTime;
    
    Var
      PosD, PosM, PosY : Integer;
      sD, sM, sY       : String;
    
    begin
    
      sd := '0';
      sm := '0';
      sy := '0';
    
      If Length(Cadena) = Length(Formato) Then
        Begin
          Formato := UpperCase(Formato);
          PosD := Pos('D',Formato);
          PosM := Pos('M',Formato);
          PosY := Pos('Y',Formato);
    
          sd := Copy(Cadena,PosD,2);
          sm := Copy(Cadena,PosM,2);
    
          if Length(Cadena) = 6 then
            begin
              sy := Copy(Cadena,PosY,2);
              if StrToInt(sy) > 50 then
                sy := '19'+sy
              else
                sy := '20'+sy;
            end
          else
            sy := Copy(Cadena,Posy,4);
        End;
      Result := EncodeDate(StrToInt(sY),
                           StrToInt(sM),
                           StrToInt(sD));
    End;
    

    greetings

    0 讨论(0)
  • 2021-02-07 05:24

    Note: Unfortunately, as pointed out by Martien in this answer's comments, this solution will not work for cases where the date includes no date separator. However, I'm going to leave the answer up for anyone that may find it useful regardless.

    I liked Tuncay's answer but there were a couple of problems with it. I'd have left a comment but I don't have enough reputation points.

    So, here's the corrected version of Tuncay's answer (amending "TFormatSetting" missing an "s" and specified the format settings date separator):

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

    Identical to Tuncay's answer, AnyStringToDate can be used as follows:

    mydate := AnyStringToDate('YYYY-MM-DD', '2015-01-20');
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题