Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: I want to know the current week number of the current month

by haukex (Archbishop)
on Oct 12, 2017 at 12:49 UTC ( [id://1201209]=note: print w/replies, xml ) Need Help??


in reply to I want to know the current week number of the current month

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

Replies are listed 'Best First'.
Re^2: I want to know the current week number of the current month
by Muskovitz (Scribe) on Oct 12, 2017 at 13:09 UTC
    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201209]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found