Trabalhando com datas !
Caros amigos; Estou precisando fazer o seguinte , tenho 2 edits , no edit 1 o usuário coloca um data desejada , exemplo : 12/01/2004 , ai em seguida automaticamente é colocado no edit 2 a seguinte data : 12/02/2004 . Exemplificando ; o meu sistema tem que colocar automaticamente uma data no edit 2 só que esta data vai ser posta a partir da som de 1 mês nda data digitada no edit 1 . OBRIGADO A TODOS ..
Tiagojmilam
Curtidas 0
Respostas
Drakkar
08/02/2004
Bem usando edits fica complicado de fazer.. entao eu usei o DateTimePicker e deu certo.
Abaixo o código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;
type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
procedure DateTimePicker1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function AnoBiSexto(Ayear: Integer): Boolean;
function DiasPorMes(Ayear, AMonth: Integer): Integer;
var
Form1: TForm1;
implementation
{$R *.dfm}
function AnoBiSexto(Ayear: Integer): Boolean;
begin
Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or
(AYear mod 400 = 0));
end;
function DiasPorMes(Ayear, AMonth: Integer): Integer;
const DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
Result := DaysInMonth[AMonth];
if (AMonth = 2) and AnoBiSexto(AYear) then
Inc(Result);
end;
procedure TForm1.DateTimePicker1Change(Sender: TObject);
Var
ano, mes, dia: word;
begin
DecodeDate(DateTimePicker1.Date,ano,mes,dia);
DateTimePicker2.Date:=DateTimePicker1.Date+DiasPorMes(ano,mes);
end;
end.
Abaixo o código:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;
type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
DateTimePicker2: TDateTimePicker;
procedure DateTimePicker1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function AnoBiSexto(Ayear: Integer): Boolean;
function DiasPorMes(Ayear, AMonth: Integer): Integer;
var
Form1: TForm1;
implementation
{$R *.dfm}
function AnoBiSexto(Ayear: Integer): Boolean;
begin
Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or
(AYear mod 400 = 0));
end;
function DiasPorMes(Ayear, AMonth: Integer): Integer;
const DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
Result := DaysInMonth[AMonth];
if (AMonth = 2) and AnoBiSexto(AYear) then
Inc(Result);
end;
procedure TForm1.DateTimePicker1Change(Sender: TObject);
Var
ano, mes, dia: word;
begin
DecodeDate(DateTimePicker1.Date,ano,mes,dia);
DateTimePicker2.Date:=DateTimePicker1.Date+DiasPorMes(ano,mes);
end;
end.
GOSTEI 0
Marco Salles
08/02/2004
Eu Gostaria De Aproveitar Este Tópico Para Questionar Sobre Ano Bissestos. Na Função Passada Pelo Nosso Amigo ´DrakkaR´ Aparece
Tres Condições Para Verificar Se o Ano é Bissexto.
function AnoBiSexto(Ayear: Integer): Boolean;
begin
Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or
(AYear mod 400 = 0));
end;
Eu Pergunto Porque Não ´Só´ Testar a Divisão Por Quatro :?: Existem Outros Anos Cujo Resto Da Divisão Por Quatro e Zero e Este Ano Não é Bissexto :?: Qual Seria Este Ano :?: :?:Para Mim Os Anos Bissestos São Sempre Multiplos De Quatro. 2004,2000,1996,1992,1988,1984,1980......
Sugiro Simplesmente o Teste :
function AnoBiSexto(Ayear: Integer): Boolean;
begin
if (AYear mod 4 = 0) Then
Result:=True
else
Result:=False;
end;
Tres Condições Para Verificar Se o Ano é Bissexto.
function AnoBiSexto(Ayear: Integer): Boolean;
begin
Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or
(AYear mod 400 = 0));
end;
Eu Pergunto Porque Não ´Só´ Testar a Divisão Por Quatro :?: Existem Outros Anos Cujo Resto Da Divisão Por Quatro e Zero e Este Ano Não é Bissexto :?: Qual Seria Este Ano :?: :?:Para Mim Os Anos Bissestos São Sempre Multiplos De Quatro. 2004,2000,1996,1992,1988,1984,1980......
Sugiro Simplesmente o Teste :
function AnoBiSexto(Ayear: Integer): Boolean;
begin
if (AYear mod 4 = 0) Then
Result:=True
else
Result:=False;
end;
GOSTEI 0
Allen74
08/02/2004
Marco,
Para esclarecer esta sua dúvida, leia neste endereço o artigo: ´A matemática dos anos bissextos´:
[url]http://www.reniza.com/matematica/novidades/0305.htm#Artigo[/url]
Para esclarecer esta sua dúvida, leia neste endereço o artigo: ´A matemática dos anos bissextos´:
[url]http://www.reniza.com/matematica/novidades/0305.htm#Artigo[/url]
GOSTEI 0
Tnaires
08/02/2004
Pessoal, ao invés d ´reinventar a roda´, usem as funções da API do Delphi. Existe uma q faz exatamente isso q vc quer: IncMonth. Vc passa uma data e ele retorna essa data incrementada na quantidade d meses q vc especificar. Ex:
IncMonth(StrToDate(´01/01/2004´), 3) retorna ´01/04/2004´.
Existe tb uma função q retorna um valor booleano indicando se o ano é bissexto ou não:
IsLeapYear(2004) retorna true;
IsLeapYear(2005) retorna false.
Essas duas funções podem ser encontradas na unit SysUtils. Na unit DateUtils ainda possuem várias outras.
Abraços.
IncMonth(StrToDate(´01/01/2004´), 3) retorna ´01/04/2004´.
Existe tb uma função q retorna um valor booleano indicando se o ano é bissexto ou não:
IsLeapYear(2004) retorna true;
IsLeapYear(2005) retorna false.
Essas duas funções podem ser encontradas na unit SysUtils. Na unit DateUtils ainda possuem várias outras.
Abraços.
GOSTEI 0