in reply to Adding to dates

And this is a DateTime solution:

#!/usr/bin/perl -l use DateTime; use DateTime::Duration; my $date = DateTime->new( year => 2006, month => 12, day => 31, locale => 'en_US', ); my $duration = DateTime::Duration->new( months => 1, end_of_month => 'preserve', ); for ( 1..12 ) { $date->add( $duration ); # One month more print $date->mdy('/'); # And show it }
Updated: With Ponky advice, rest:
use DateTime; my $date = DateTime->last_day_of_month( year => 2006, month => 12 ); print $date->add( months => 1, end_of_month => 'preserve' )->mdy('/') +foreach 1..12;

Replies are listed 'Best First'.
Re^2: Adding to dates
by Ponky (Curate) on Aug 23, 2006 at 02:38 UTC
    A little improvement is to include the duration definition in the add call:
    $date->add( months => 1, end_of_month => 'preserve' );
    Then you don't need the use DateTime::Duration either.