Muskovitz has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!

#!C:\xampp\perl\bin\perl.exe use strict; use warnings; use Date::Calc qw/Week_Number/; use POSIX qw/strftime/; my($yy,$mm,$dd)=Today(); my $DATE=strftime "%b %Y", localtime(); my $WEEKNUM=Week_Number($yy, $mm, $dd); print "It is Week: $WEEKNUM As of $DATE<br />";
I am expecting the result would be It is Week: 2 As of Oct 2017 but instead i get It is Week: 41 As of Oct 2017

I want to get the week number of the current month so i can know if it is 2nd week of october or 3rd and so on ...

Replies are listed 'Best First'.
Re: I want to know the current week number of the current month
by haukex (Archbishop) on Oct 12, 2017 at 12:49 UTC

    I might be missing something, but I don't see a function in Date::Calc. Here's a DateTime solution:

    use DateTime; my $dt = DateTime->now; my $date = $dt->strftime("%b %Y"); my $week = $dt->week_of_month(); print "It is Week: $week As of $date\n"; __END__ It is Week: 2 As of Oct 2017

    However, note that the week number returned here can be zero - from the DateTime docs on week_of_month:

    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.

    So you need to have a clear definition of what the Nth week means for you. (Update: LanX made the same point here.)

      Thanks!
      As for now i think this will do!
      Later on if i encounter some "0's" stuff i think i'll just sort it out with some conditional statements
        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

        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) ...

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

        Hello again Muskovitz.

        Do check my latest update (update3) bellow.

        From my point of view if you want to calculate correctly the number of weeks is 3. Because this month started on Sunday (1s of October). Which means the second week was 2nd day of the month. So since we are in the 12th of October currently it makes sense to be the third week, if you calculating the weeks as they start on Monday.

        So at this point it depends on what you want to consider a week. Do you count weeks e.g. if the date is 12th then only two weeks have passed this month what if the month has 31 days and the month started on Sunday as on this case then we have 6 weeks in total if you count the beginning of a week on Monday.

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: I want to know the current week number of the current month
by LanX (Saint) on Oct 12, 2017 at 12:47 UTC
    Week of year is already fuzzy because depending on regional conventions, Excell for instance provides multiple definitions for different standards.

    But I'm puzzled about how do you define week of months?

    edit

    If you don't know what I mean then better do a subtraction

    $week_current - $week_first_day_of_month + 1

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Yes! exactly Week of Months.
      Uhmm ... to be clear. I am currently making a Billing System using Perl and i need also to put Weekly totals for example: As for 1st Week of October: $2,100, As for 2nd Week of October: $3,211 ... etc ...
        Thanks for ignoring my question (again)

        Simplified:

        • Which week did 1. of October (Sunday) and 2. belong to?
        • What if the 1. was a Wednesday?
        • Are you aware that most months don't start on Mondays?
        update
        • Are you aware that most months have more than 28 (= 7*4) days?

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: I want to know the current week number of the current month
by hdb (Monsignor) on Oct 13, 2017 at 06:05 UTC

    This thread is a fantastic piece of Calendar Philosophy! I love it.

    It throws a nice light on our interesting conventions on counting days, weeks and months relating to the solar and lunar calendars. Why can't the universe be more organized and ensures that the length of our year and moon cycles are proper multiples of the length of an earth day?

    My proposal would be to start each year with a Sunday and end it on a Saturday. This would give 364 days for each year. The time between the last Saturday of a year and the first Sunday of the following should be given some special name and be a global holiday of one or two days length. We can then divide the year into 13 months of 4 weeks each.

    I am aware that this will not solve all types of calendar calculations and the lunar calendar will not align to this scheme. It will also not be compatible with a number of religions who rely on an uninterrupted cycle of 7-day weeks or the interaction between the solar and lunar calendars.

    There is also no global organization who will be able to get everybody to agree to such a scheme and it will lead to even more conflict in the world...

    Which brings us back to the point where Muskovitz needs to define the current week number of the current month.

    Thanks for listening to my ramble on this Friday, 13th (Gregorian Calendar), 2nd week of October 2017.

      > My proposal would be to start

      Actually that idea is much older than you might think, see Egyptian calendar

      With the difference that the "week" in Egypt had 10 days.

      The desire for a 7 day week stems IIRC from Babylon in order to split the moon cycle in four parts (well almost) and was adopted and spread by the monotheistic book religions.

      Getting 3 unrelated astronomical rotation cycles (earth day, lunar month, solar year) in sync with a table can't be easy. :)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: I want to know the current week number of the current month (UPDATED)
by thanos1983 (Parson) on Oct 12, 2017 at 14:08 UTC

    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!
Re: I want to know the current week number of the current month
by karlgoethebier (Abbot) on Oct 12, 2017 at 17:40 UTC
    #!/usr/bin/env perl use strict; use warnings; use Date::Calc qw(Today Week_Number); use feature qw(say); say Week_Number( Today() ); __END__

    This gives 41 which seems to be right IMHO.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help