Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Counting number of a particular day of the month.

by ninja-joe (Monk)
on May 31, 2002 at 20:16 UTC ( [id://170821]=perlquestion: print w/replies, xml ) Need Help??

ninja-joe has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a billing system for a musical instruction facility. They have students that come in on a weekly basis on a particular day. Let's say for instance this day is Wenesday. Some months will either have four or five of Wenesdays depending on what type of day the month begins on.

Here's my method (assuming the day of the week would be Wenesday):
Given: $n = # of days so far
1. Find the date on which the first Wenesday of that month would occur ($n=1);
2. Add 7 to that date, if the date is within the current month you're trying to calculate then increment $n ($n++);
3. If the date is beyond the interested month, exit the loop.

That's a pretty brute forced method. Anybody have any better ideas? Is there a module that any knows of that will count it for me? It may be too simple of a task to really need a better method too.
  • Comment on Counting number of a particular day of the month.

Replies are listed 'Best First'.
Re: Counting number of a particular day of the month.
by jmcnamara (Monsignor) on May 31, 2002 at 23:28 UTC

    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.

      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?
Re: Counting number of a particular day of the month.
by Rich36 (Chaplain) on May 31, 2002 at 20:20 UTC
    While I don't think there's a built-in function for what you're looking for, the module Date::Calc provides a whole range of functions for dealing with dates and times. I'm sure there are things in there that would be useful.
    Rich36
    There's more than one way to screw it up...

Re: Counting number of a particular day of the month.
by cjf (Parson) on May 31, 2002 at 20:23 UTC

    Date::Calc will do the job:

    use Date::Calc qw/:all/; my $days = Days_in_Month($year,$month)

    Check the docs for many more useful functions.

    Update: Note to self, finish reading question before answering :)

    You should take a look at the Day_of_Week() function, using that and the number of days in the month you can loop over the month and calculate the number of the days you're looking for. It isn't pretty, but it should do.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found