[Pgsql-ayuda] Ultimo dia del mes

Alvaro Herrera alvherre@dcc.uchile.cl
Sat, 23 Aug 2003 13:14:36 -0400


On Sat, Aug 23, 2003 at 10:54:20AM -0600, Josué Maldonado wrote:
> Hola lista,
> 
> Como puedo obtener el ultimo dia del mes de un fecha dada?

En SQL:

select extract(day from (extract(year from now()) || '-' ||
extract(month from now()) + 1  || '-01')::date - '1 day'::interval);

(seguramente hay una forma mejor)

O en Perl:

sub diasDelMes {
	my $mes = shift;
	my $anno = shift;
	return undef unless defined $anno && defined $mes;
	if ($mes == 2) {
		return 28 unless ($anno % 4 == 0) xor
			($anno % 100 == 0) xor ($anno % 400 == 0);
		return 29;
	}
	return (qw(0 31 0 31 30 31 30 31 31 30 31 30 31))[$mes];
}

(esto es trivialmente traducible a cualquier lenguaje imperativo)

-- 
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"At least to kernel hackers, who really are human, despite occasional
rumors to the contrary" (LWN.net)