Buscar

FormatStringToDate - Tranformar de String para Date passando o formato da string por parâmetro

Código

function FormatStringToDate(Format, strDate: String): TDateTime;
const
   CH_DAY = 'D';
   CH_MON = 'M';
   CH_YEA = 'Y';
var
   vDay, vMon, vYea: String;

   function StrCountChar(Value: Char; Str: String): Integer;
   var i: Integer;
   begin
      Result := 0;
      for i := 0 to Length(Str) do
         if Str[i] = Value then
            Inc(Result);
   end;

   function StrOnlyNumber(Str: String): String;
   var i: Integer;
   begin
      Result := '';
      for i := 0 to Length(Str) do
         if Str[i] in ['0'..'9'] then
            Result := Result + Str[i];
   end;

begin
   Format := UpperCase(Format);

   vDay := StrOnlyNumber(Copy(strDate,Pos(CH_DAY,Format),StrCountChar(CH_DAY,Format)));
   vMon := StrOnlyNumber(Copy(strDate,Pos(CH_MON,Format),StrCountChar(CH_MON,Format)));
   vYea := StrOnlyNumber(Copy(strDate,Pos(CH_YEA,Format),StrCountChar(CH_YEA,Format)));

   Result := EncodeDate(StrToInt(vYea),StrToInt(vMon),StrToInt(vDay));
end;

Publicidade

Vote na dica




Quantidade de votos: 1 voto
Aceitação: 20%


Detalhes da dica

Categoria: Object pascal
Adicionada dia: 26/06/09
Por: Fábio Romão De Oliveira
Visualizada: 2870 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009