tags:

views:

244

answers:

5

i am looking an funtion to reverse any string (YYYYMDD,YY/MM/DD,YYMMDD,...) created by the function FormatDateTime to datetime.

example

i have a string-date in format YYYYMMDDcreated by FormatDateTime

mydatestr:=FormatDateTime('YYYYMMDD',Mydate); 

now how i can convert mydatestr to DateTime again?

UPDATE

these functions

function StrToDate(const S: string): TDateTime; overload;
function StrToDate(const S: string;
  const FormatSettings: TFormatSettings): TDateTime; overload;

function StrToDateTime(const S: string): TDateTime; overload;
function StrToDateTime(const S: string;
  const FormatSettings: TFormatSettings): TDateTime; overload;

does not support passing an string wich the format to convert.

i am looking somethign like this

Mydatetime:=InvFormatDatetime('20091225','yyyymmdd');

or

Mydatetime:=InvFormatDatetime('20090108','yyyyddmm');
+1  A: 

Did you check StrToDate and StrToDateTime ?

Liz Albin
Gah! I've been ninja'd! x.x +1
Mason Wheeler
@Liz, i knew these functions, and they not support the functionality wich i am looking for.
Salvador
A: 

Take a look at StrToDateTime and the related functions in SysUtils. One of them should be able to do what you need.

Mason Wheeler
+3  A: 

It is quire 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);
too
thanks very much, the function works very well.
Salvador
A: 
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

Job Espejel
This function assumes that the only day and month formats used are DD and MM, when they can in fact be between 1 and 4 characters long, and any format amy appear more than once. This code also ignores `TwoDigitYearCenturyWindow`.
Rob Kennedy
A: 

Normally, I would just insert the characters needed to get StrToDate to work.

Marcus Adams