Hello Muskovitz,

Fellow Monks has answered your question but just for reference, your question was asked before Getting the current week number.

I recently find a new module Date::Calc that is simply amazing powerful. See sample of code below:

#!/usr/bin/perl use strict; use warnings; use Date::Calc qw( Day_of_Week Delta_Days Nth_Weekday_of_Month_Year Date_to_Text_Long English_Ordinal Day_of_Week_to_Text Month_to_Text Today Week_Number); my ($year,$month,$day) = Today(); my $dow = Day_of_Week($year,$month,$day); my $n = int( Delta_Days( Nth_Weekday_of_Month_Year($year,$month,$dow,1), $year,$month,$day) / 7) + 1; printf("%s is the %s %s in %s %d.\n", Date_to_Text_Long($year,$month,$day), English_Ordinal($n), Day_of_Week_to_Text($dow), Month_to_Text($month), $year); printf("Just FYI the week number is %d in %d.\n", Week_Number($year, $month, $day), $year); __END__ $ perl test.pl Thursday, October 12th 2017 is the 2nd Thursday in October 2017. Just FYI the week number is 41 in 2017.

If I was you I would simply calculate and extract the number of the function $string = English_Ordinal($number); see Date::Calc.

Update: Just in case that my explanation is a bit confusing use this calculation (it will give you the week number):

my $n = int( Delta_Days( Nth_Weekday_of_Month_Year($year, $month, $dow, 1), $year, $month, $day) / 7) + 1; say "Number: " . $n; __END__ Number: 2

Update2: I think that this solution also might give you the number of occurrences in the week e.g. 2nd which from this number you can calculate the week number. But in case that you fall into the exception e.g. Sunday is appearing in 5th time in the month. Sample from documentation Is Sunday, the 15th of October 2000, the 1st, 2nd, 3rd, 4th or 5th Sunday of that month? source Date::Calc/RECIPES.

Update3: From the documentation How do I calculate the number of the week of month the current date lies in?.

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Date::Calc qw( Today Day_of_Week ); my ($year,$month,$day) = Today(); my $week = int(($day + Day_of_Week($year,$month,1) - 2) / 7) + 1; say "Week number: " . $week; __END__ $ perl test.pl Week number: 3

Why we see the number 3 instead of 2 since the date today is 12/10/2017. Because the month started on Sunday first of the month, so the next weeks including the current are 2 plus the week that the month started we have 3 in total.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: I want to know the current week number of the current month (UPDATED) by thanos1983
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.