Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

No: of weeks in a month

by Anonymous Monk
on Sep 13, 2012 at 07:27 UTC ( #993393=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, Can any one please guide me to find the number of weeks in the current month?Is there any Perl Module which helps us in simple coding? Thanks

Replies are listed 'Best First'.
Re: No: of weeks in a month
by Anonymous Monk on Sep 13, 2012 at 11:45 UTC
    The question you ask is ambiguous:   September 2013 begins on a Saturday and ends the following Sunday ... how many weeks, to you, is that?   You might say six, or you might say four.   Date-calculation routines are available in abundance on CPAN, but first you must nail-down the exact meaning of the question for you.   See for example Date::Calc, Date::Manip for the math heavy-lifting.

      Or you might say 30/7 == 4 and 2/7.

      You might even say 5, if your definition of a week being in a month is that it begins in that month - those being the weeks beginning on the 2nd, 9th, 16th, 23rd and 30th.

      The question you ask is ambiguous
      No, it isn't. We have a standard that defines what a week is.

        But that ISO standard has no definition of what defines weeks per month.

        A week is clearly defined but not how it relates to the entity of the month. I think it's safe to assume that the OP meant it to be the number of weeks that either fall completely within the month and those that are transected by the beginning and end of the month.

Re: No: of weeks in a month
by grumbert (Scribe) on Sep 13, 2012 at 11:25 UTC
    I assume you are interested in the number of calendar weeks a month straddles rather than a simplified "February has 28 days so only 4 weeks in February..."

    I don't really know of a module that gives that answer but Date::Calc has a Week_of_Year function which you could utilise (use it on first and last day of the month , then subtract the weeknumbers and add one), which should give you the number you're looking for...

      Thanks for the reply :) will try that.
Re: No: of weeks in a month
by Anonymous Monk on Sep 13, 2012 at 07:33 UTC

      Hmm, I am trying to explain.

      my %daysinmonth=(Jan=>31,Feb=>30,Mar=>31,Apr=>30,May=>31,Jun=>30,Jul=> +31,Aug=>31,Sep=>30,Oct=>31,Nov=>30,Dec=>31); $weeks_count= $daysinmonth{enteryourmonth}/7; $day_count= $daysinmonth{enteryourmonth}-($weeks_count*7); print "Only $weeks_count weeks in $daysinmonth{enteryourmonth} month";

      I have asked for some specific date module which help in this calculation

        DateTime is the Swiss army chainsaw of date calculations.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993393]
Front-paged by Arunbear
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-04-23 00:29 GMT
Find Nodes?
    Voting Booth?