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
public
end;
function Valida_Date1(Day, Month, Year : Integer) : Boolean;
function Valida_Date2(Day, Month, Year : Integer) : Boolean;
var
Form1: TForm1;
implementation
{$R *.dfm}
function Valida_Date1(Day, Month, Year : Integer) : Boolean;
var
AuxYear : Boolean;
begin
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;
if (Month < 1) OR (Month > 12) then
begin
ShowMessage('Mes Invalido');
Result := False;
Exit;
end;
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;
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;
function Valida_Date2(Day, Month, Year : Integer) : Boolean;
var
ValDate : TDateTime;
begin
Try
ValDate := EncodeDate(Year, Month, Day);
Except
Result := False;
Exit;
end;
Result := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MaskEdit1.EditMask := '00-00-0000';
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));
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));
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