Inverse function of FormatDateTime

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

提交回复
热议问题