Here is a solution using Date::Calc, which could be shortened up a bit. I hard coded a range of dates to serve as an example:

use warnings; use strict; use Date::Calc qw( Week_of_Year Monday_of_Week Month_to_Text ); # Date::Calc uses 1 = Monday, ... 7 = Sunday foreach my $date ( 1 .. 20 ) { my $week = Week_of_Year( 2005, 4, $date ); # see docs for caveats my ( $year, $month, $day ) = Monday_of_Week( $week, 2005 ); print "The Monday before April $date was ", Month_to_Text( $month ), " $day\n"; }

Output:

The Monday before April 1 was March 28 The Monday before April 2 was March 28 The Monday before April 3 was March 28 The Monday before April 4 was April 4 The Monday before April 5 was April 4 The Monday before April 6 was April 4 The Monday before April 7 was April 4 The Monday before April 8 was April 4 The Monday before April 9 was April 4 The Monday before April 10 was April 4 The Monday before April 11 was April 11 The Monday before April 12 was April 11 The Monday before April 13 was April 11 The Monday before April 14 was April 11 The Monday before April 15 was April 11 The Monday before April 16 was April 11 The Monday before April 17 was April 11 The Monday before April 18 was April 18 The Monday before April 19 was April 18 The Monday before April 20 was April 18

Once you know the starting date, it is trivial to calculate the rest. :)

HTH


In reply to Re: Calculating the "nearest" week by bobf
in thread Calculating the "nearest" week by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.