numero de dias de um mes
24/11/2005
0
Preciso de uma funcao para retornar o número de dias de um mês. Tente a função abaixo mas dá erro:
function DiasNoMes(AYear, AMonth: Integer): Integer;
const
DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
if AMonth = 2 then
begin
if Ayear mod 4 = 0 then
begin
Inc(DaysInMonth[AMonth]); /////dá erro aqui /////
end;
end;
Result := DaysInMonth[AMonth];
end;
function DiasNoMes(AYear, AMonth: Integer): Integer;
const
DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
if AMonth = 2 then
begin
if Ayear mod 4 = 0 then
begin
Inc(DaysInMonth[AMonth]); /////dá erro aqui /////
end;
end;
Result := DaysInMonth[AMonth];
end;
Roberto Rossi
Curtir tópico
+ 0
Responder
Posts
24/11/2005
Edilcimar
vc não tem que criar a função que já existe daysinmonth, ela por si só já retorna a quantidade de dias do mês, você está tentando atribuir 31 dias para uma mês que pode não ter 31
Responder
24/11/2005
Massuda
Talvez uma abordagem diferente evite reinventar a roda... na unit SysUtils existe um array com os dias do mês chamado [b:42431898cf]MonthDays[/b:42431898cf] e uma função para testar se um ano é bissexto chamada IsLeapYear. Assim sua função ficaria assim......ou simplesmente nem use a função.
function DiasNoMes(AYear, AMonth: Integer): Integer; begin result := MonthDays[IsLeapYear(AYear), AMonth]; end;
Responder
Clique aqui para fazer login e interagir na Comunidade :)