Ver Mensaje Individual
  #4  
Antiguo 23-10-2012
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
cesar.zapata,

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    MaskEdit1: TMaskEdit;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function Valida_Date1(Day, Month, Year : Integer) : Boolean;
  function Valida_Date2(Day, Month, Year : Integer) : Boolean;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Función 1 de Validación de Fecha
function Valida_Date1(Day, Month, Year : Integer) : Boolean;
var
   AuxYear : Boolean;

begin

  // Valida que Dia, Mes y Año sean diferente de cero

  if Day = 0 then
   begin
      ShowMessage('Día Invalido');
      Result := False;
      Exit;
   end;

   if Month = 0 then
   begin
      ShowMessage('Mes Invalido');
      Result := False;
      Exit;
   end;

   if Year = 0 then
   begin
      ShowMessage('Año Invalido');
      Result := False;
      Exit;
   end;

   // Valida que el mes este entre 1 y 12
   if (Month < 1) OR (Month > 12) then
   begin
      ShowMessage('Mes Invalido');
      Result := False;
      Exit;
   end;

   // Valida si el Año es Bisiesto
   AuxYear := False;

   if ((Year Mod 4) = 0) and ((Year Mod 100) <> 0) then
   begin
      AuxYear := True;
   end;

   if ((Year Mod 4) = 0) and ((Year Mod 100) = 0) and ((Year Mod 400) = 0) then
   begin
      AuxYear := True;
   end;

   // Valida que el rango correcto de días según el mes
   case Month of
       9, 4, 6, 11:
          if (Day < 1) or (Day > 30) then
          begin
             ShowMessage('Día Invalido');
             Result := False;
             Exit;
          end;
       2:
          if (Month = 2) and (AuxYear) then
          begin
             if (Day < 1) or (Day > 29) then
             begin
                ShowMessage('Día Invalido');
                Result := False;
                Exit;
             end;
          end
          else
          begin
             if (Day < 1) or (Day > 28) then
             begin
                ShowMessage('Día Invalido');
                Result := False;
                Exit;
             end;
          end;
       1, 3, 5, 7, 8, 10, 12:
          if (Day < 1) or (Day > 31) then
          begin
             ShowMessage('Día Invalido');
             Result := False;
             Exit;
          end;
   end;

   Result := True;

end;

// Función 2 de Validación de Fecha
function Valida_Date2(Day, Month, Year : Integer) : Boolean;
var
  ValDate : TDateTime;
begin

  Try
     // Usa la función EncodeDate para validar la fecha
     ValDate := EncodeDate(Year, Month, Day);
  Except
        Result := False;
        Exit;
  end;

  Result := True;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  // Formatea la fecha
  MaskEdit1.EditMask := '00-00-0000';

  // Coloca la fecha actual
  MaskEdit1.Text :=  FormatDateTime('dd/mm/yyyy', Now);

  MaskEdit1.AutoSelect := False;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
   StrDate : String;
   Day, Month, Year : Integer;
begin

   StrDate := MaskEdit1.Text;
   Day := StrToInt(Copy(StrDate,1,2));
   Month := StrToInt(Copy(StrDate,4,2));
   Year := StrToInt(Copy(StrDate,7,4));

   // Validata la fecha en forma programática detallada (Tipo Pascal)
   If Not Valida_Date1(Day,Month,Year) then ShowMessage('Fecha Invalida');

end;

procedure TForm1.Button2Click(Sender: TObject);
var
   StrDate : String;
   Day, Month, Year : Integer;
begin

   StrDate := MaskEdit1.Text;
   Day := StrToInt(Copy(StrDate,1,2));
   Month := StrToInt(Copy(StrDate,4,2));
   Year := StrToInt(Copy(StrDate,7,4));

   // Valida la fecha usando la función EncodeDate
   If Not Valida_Date2(Day,Month,Year) then ShowMessage('Fecha Invalida');

end;

end.
En el código anterior tienes dos funciones de validación de fechas, manejo de Strings y uso del control MaskEdit que te sirviran de base para tu proyecto.

Espero sea útil

Nelson

Última edición por nlsgarcia fecha: 23-10-2012 a las 01:12:33.
Responder Con Cita