#!/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!
|