Later on if i encounter some "0's" stuff i think i'll just sort it out with some conditional statements

I'm not sure what you mean by this. If you don't want to have a week numbered zero, you could just add one to the week number, but keep in mind that some months will end up having up to six weeks and the first and last "weeks" will almost always end up having less than seven days:

_________ Oct 2017 _________ Week 1: 01 Week 2: 02 03 04 05 06 07 08 Week 3: 09 10 11 12 13 14 15 Week 4: 16 17 18 19 20 21 22 Week 5: 23 24 25 26 27 28 29 Week 6: 30 31
use warnings; use strict; use DateTime; my $year = 2017; for my $month (1..12) { my $dt = DateTime->new(year=>$year,month=>$month); print "_"x9,$dt->strftime(" %b %Y "),"_"x9,"\n"; my $prevweek=-1; while ($dt->month==$month) { my $week = $dt->week_of_month(); print $prevweek<0?'':"\n", "Week ",$week+1,":" if $week != $prevweek; printf " %02d", $dt->day; $prevweek=$week; $dt->add(days=>1); } print "\n"; }

As others have said, talk to your client / boss about how they define "week of month". A common system is the ISO 8601 week number, maybe your client / boss would be happier with that system, since every week will have seven days. Also note in the following table how some weeks have two week_of_month-based "names" under the above system.

________________________ 2017 ________________________ Week Mon Tue Wed Thu Fri Sat Sun 52 Dec 26 27 28 29 30 31 Jan 01 (Dec Week 6, +Jan Week 1) 1 02 03 04 05 06 07 08 (Jan Week 2) 2 09 10 11 12 13 14 15 (Jan Week 3) 3 16 17 18 19 20 21 22 (Jan Week 4) 4 23 24 25 26 27 28 29 (Jan Week 5) 5 30 31 Feb 01 02 03 04 05 (Feb Week 2, +Jan Week 6) ... 39 25 26 27 28 29 30 Oct 01 (Oct Week 1, +Sep Week 5) 40 02 03 04 05 06 07 08 (Oct Week 2) 41 09 10 11 12 13 14 15 (Oct Week 3) 42 16 17 18 19 20 21 22 (Oct Week 4) 43 23 24 25 26 27 28 29 (Oct Week 5) 44 30 31 Nov 01 02 03 04 05 (Nov Week 2, +Oct Week 6) ...
use warnings; use strict; use DateTime; my $year = 2017; my $dt = DateTime->new(year=>$year)->truncate(to=>'week'); print "_"x24," $year ","_"x24,"\n"; my $dt1 = $dt->clone; print "Week "; for (1..7) { print " ",$dt1->day_abbr; $dt1->add(days=>1); } print "\n"; my $prevmonth=0; for (1..54) { # weeks my $weekno = $dt->week_number; printf "%4d ", $weekno; my %names; for (1..7) { # days in the week die unless $dt->week_number==$weekno; printf " %3s %02d", $prevmonth==$dt->month?'':$dt->month_abbr, $dt->day; $names{$dt->month_abbr." Week ".($dt->week_of_month+1)}++; $prevmonth=$dt->month; $dt->add(days=>1); } print " (".join(', ',sort keys %names),")\n"; }

Update: Fixed bug that weekdays weren't properly aligned. Also added the week_of_month-based "names".


In reply to Re^3: I want to know the current week number of the current month (updated!) by haukex
in thread I want to know the current week number of the current month by Muskovitz

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.