Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Adding to dates

by explorer (Chaplain)
on Aug 23, 2006 at 02:29 UTC ( #569008=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://569008]
and !@monks...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-12-17 09:03 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (462 votes). Check out past polls.