Part of the challenge is how you are defining the week numbering. For example, April 1, 2016 falls on a Friday. So is April, 1, 2016 in week 0? Or is it week 1?

Depending on your needs, you can check out DateTime's week_of_month method, which is described as:

The week of the month, from 0..5. The first week of the month is the first week that contains a Thursday. This is based on the ICU definition of week of month, and correlates to the ISO8601 week of year definition. A day in the week before the week with the first Thursday will be week 0.

Here's an example code that displays shows some details (name of day, name of month, week of month) for 40 dates starting with the current date.

use strict; use warnings; use feature 'say'; use DateTime; my $dt = DateTime->now; foreach my $count (1..40) { my $day = $dt->day_name; my $month = $dt->month_name; my $week_of_month = $dt->week_of_month; say "Date/time stamp: $dt"; say " Name of Day: $day"; say " Name of Month: $month"; say " Week of Month: $week_of_month"; say ""; my $dur = DateTime::Duration->new(days => 1); $dt = $dt->add_duration($dur); }

Of course, if that does not match the way that you want to determine which week number of the month that a particular date is in, then you may need to come up with some custom code to better match your needs.


In reply to Re: Week number of the month by dasgar
in thread Week number of the month by Anonymous Monk

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.