Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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;


Comment on Re: Adding to dates
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://569008]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-09-19 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (145 votes), past polls