Fórum DateTimePicker #179382
03/09/2003
0
Como faço para retornar o último dia daquele mês daquele ano
Exemplo 01: Informei a data Fevereiro de 2000
Retorno <--- 29
Exemplo 02: Informei a data Fevereiro de 2001
Retorno <--- 28
Exemplo 01: Informei a data Fevereiro de 2000
Retorno <--- 29
Exemplo 02: Informei a data Fevereiro de 2001
Retorno <--- 28
Marcela
Curtir tópico
+ 0
Responder
Posts
09/09/2003
Mmtoor
Prezada colega:
Se tivesse seu mail mandaria um exemplo com fonte, mas pode analisar e criar o seu com a unit abaixo?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function UltimodoMes(wMes, wAno : Word) : Word;
var
UltMes : TDateTime;
begin
if wMes > 12 then
raise ERangeError.Create(´Mês inválido´);
// mês seguinte
Inc(wMes);
// se mês > 12 incrementa o ano e volta mês a 1
if wMes > 12 then begin
wMes := 1;
Inc(wAno);
end;
// pega data do último do mês
UltMes := EncodeDate(wAno,wMes,1)-1;
// decodifica para pegar o último dia do mês
DecodeDate(UltMes,wAno,wMes,Result);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
ShowMessage(´Último Dia: ´+IntToStr(UltimodoMes(StrToInt(Edit1.Text),StrToInt(Edit2.Text))));
except
On EConvertError do
MessageDlg(´Um dos valores não é valido´,mtError,[mbOk],0);
end;
end;
end.
MMTOOR2003
Se tivesse seu mail mandaria um exemplo com fonte, mas pode analisar e criar o seu com a unit abaixo?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function UltimodoMes(wMes, wAno : Word) : Word;
var
UltMes : TDateTime;
begin
if wMes > 12 then
raise ERangeError.Create(´Mês inválido´);
// mês seguinte
Inc(wMes);
// se mês > 12 incrementa o ano e volta mês a 1
if wMes > 12 then begin
wMes := 1;
Inc(wAno);
end;
// pega data do último do mês
UltMes := EncodeDate(wAno,wMes,1)-1;
// decodifica para pegar o último dia do mês
DecodeDate(UltMes,wAno,wMes,Result);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
ShowMessage(´Último Dia: ´+IntToStr(UltimodoMes(StrToInt(Edit1.Text),StrToInt(Edit2.Text))));
except
On EConvertError do
MessageDlg(´Um dos valores não é valido´,mtError,[mbOk],0);
end;
end;
end.
MMTOOR2003
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)