in reply to Counting number of a particular day of the month.


Here is one way using Date::Calc:
#!/usr/bin/perl -wl use strict; use Date::Calc 'Nth_Weekday_of_Month_Year'; sub max_days_in_month { my $day = $_[0]; # Monday is 1 my $month = $_[1]; # January is 1 my $year = $_[2]; scalar grep {Nth_Weekday_of_Month_Year($year, $month, $day, $_ +)} 1..5; } print "There are ", max_days_in_month(1, 6, 2002), " Mondays in Ju +ne 2002."; print "There are ", max_days_in_month(6, 6, 2002), " Sundays in Ju +ne 2002."; __END__ Prints: There are 4 Mondays in June 2002. There are 5 Sundays in June 2002. (Good news for those for prefer Sundays to Mondays)

--
John.

Replies are listed 'Best First'.
Re: Re: Counting number of a particular day of the month.
by ninja-joe (Monk) on Jun 01, 2002 at 14:21 UTC
    I got it. This one is a pretty lighthanded way to do it I think (cheap but effective):
    use strict; use lib '~/mylib/lib/site_perl/5.6.0'; use Date::Calc qw( Day_of_Week Day_of_Week_to_Text Days_in_Month Month_to_Text ); my $searchday = 2; # Tuesday my $month = 5; # May my $year = 2002; my $numdays = Days_in_Month($year,$month); my $count = 0; for(my $date=1;$date<=$numdays;$date++) { if($searchday == (Day_of_Week($year,$month,$date)) ) { $count +++; } } print "There are $count ".Day_of_Week_to_Text($searchday)."s in ".Mont +h_to_Text($month).".\n";

    Can you dig it?