Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

List of days in current month

by Anonymous Monk
on Mar 13, 2014 at 18:19 UTC ( [id://1078219]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to build a list of the days in the current month using Time::Piece, but I am stuck, here is what I am trying to do or a prototype of it:
#!/usr/bin/perl use strict; use warnings; use Time::Piece; my $t = Time::Piece->new; my $month = $t->mon; my $year = $t->year; for my $days ($month) { my $dates = Time::Piece->strptime("$month/$days/$year" . '01', '% +m%d%Y%')->wday); }

At the end I am trying to print a list for the current month as:
03/01/2014 03/02/2014 ... 03/31/2014

Thanks for the help!

Replies are listed 'Best First'.
Re: List of days in current month
by toolic (Bishop) on Mar 13, 2014 at 18:38 UTC
    use strict; use warnings; use Time::Piece qw(); my $t = Time::Piece->new(); my $month = $t->mon; my $year = $t->year; my $lday = $t->month_last_day; for (1 .. $lday) { printf "%02d/%02d/%04d\n", $month, $_, $year; } __END__ 03/01/2014 03/02/2014 ... 03/31/2014
      Thanks, that pointed me to the right direction:
      #!/usr/bin/env perl use strict; use warnings; use Time::Piece qw(); my $t = Time::Piece->new(); my $month = $t->mon; my $year = $t->year; my $lday = $t->month_last_day; my @dates = map sprintf( "%02d/%02d/%04d", $month, $_, $year ), 1 .. $ +lday; foreach my $date(@dates) { print $date."\n"; }
Re: List of days in current month
by Limbic~Region (Chancellor) on Mar 13, 2014 at 18:36 UTC
Re: List of days in current month
by ambrus (Abbot) on Apr 18, 2014 at 12:39 UTC

    Try to do the calculations with the Date::Manip module.

    use warnings; use strict; use Date::Manip::Date 6.30; my $today = Date::Manip::Date->new("today"); my $month_first = $today->new($today->printf("%Y-%m-01")); my $next_month_first = $month_first->calc($today->new_delta("+1 month" +)); my $offset = $today->new_delta("+1 day"); for (my $day = $month_first; $day->cmp($next_month_first) < 0; $day = $day->calc($offset)) { print $day->printf("%m/%d/%Y\n"); } __END__

    This outputs:

    04/01/2014 04/02/2014 04/03/2014 04/04/2014 04/05/2014 04/06/2014 04/07/2014 04/08/2014 04/09/2014 04/10/2014 04/11/2014 04/12/2014 04/13/2014 04/14/2014 04/15/2014 04/16/2014 04/17/2014 04/18/2014 04/19/2014 04/20/2014 04/21/2014 04/22/2014 04/23/2014 04/24/2014 04/25/2014 04/26/2014 04/27/2014 04/28/2014 04/29/2014 04/30/2014
Re: List of days in current month
by Lennotoecom (Pilgrim) on Mar 14, 2014 at 06:59 UTC
    As an option
    ($m, $d) = (localtime)[4, 3]; $b = time - ($d * 86400); while((localtime($b+=86400))[4] == $m){ print "",(localtime($b))[3],"\n"; }
      Not every day has 86400 seconds.
        Which ones dont?
      dear critics if you minus,
      please be a dear and write down at least a little description
      exactly why. ty
      because simple haters are stupid, aren't they?
      update
      exactly what I thought

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1078219]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found