+ Reply to Thread
Results 1 to 8 of 8

Getting Week Number within month time frame: why 13 and -5?

  1. #1
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126

    Question Getting Week Number within month time frame: why 13 and -5?

    Please Login or Register  to view this content.
    I got this piece of code from SO. It works fine; in trying to understand the logic of how it works:

    - why 13 is being added (not 14)
    - why date is reduced by -5 in particular

    It will tell me the correct week number within this month (e.g. 01/06/22 will be 1 week number) but I am trying to understand the logic for my knowledge sake.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,911

    Re: Getting Week Number within month time frame: why 13 and -5?

    Do your weeks begin on Friday? If not, I'm not sure that function works.
    Rory

  3. #3
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Getting Week Number within month time frame: why 13 and -5?

    Let's break it down for the 1st January 2022:

    Day() will return 1 (i.e. the 1st day of January)
    Weekday() would return 7 (Saturday) but moving this back by 5 days will return 2 (Tuesday)
    So we then have (13+1-2)/7=1
    Similarly, for the 2nd, 3rd, 4th, 5th, 6th we would have (13+2-3)/7=1, (13+3-4)/7=1, (13+4-5)/7=1, (13+5-6)/7=1, (13+6-7)/7=1

    On the 7th, it will wrap around to give (13+7-1)/7 which is now =2. I assume this function was written to force new week numbers to start on a Friday.

    WBD
    Office 365 on Windows 11, looking for rep!

  4. #4
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Getting Week Number within month time frame: why 13 and -5?

    if you want your function to have Sunday start the week... use:

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: Getting Week Number within month time frame: why 13 and -5?

    Let me add also my explanation.
    The function is planned for weeks (it's best visible for week 2,3,4) starting Friday / ending Thursday

    The -5 (it could be as well +2 ! try it !) is used to get 7 (last day of given week) on Thursdays
    (Have you noted that dat-5 is used as argument for Weekday function.)
    Of course 7 is the largest value we can get from weekday so for any non-ending_of_week day we will get lower number. But never lower than 1 - which we wll get for Fridays

    Lets look on a sum of some_number plus 1 minus 7 (one is for the first day of month, 7 is for the largest possible resuly of weekday - as said above) and we want it to be exactly 7 (because we will divide it by 7 and drop fraction part).

    some_number + 1 - 7 = 7
    so
    some_number = 7 -1 + 7
    =13

    Hope it helps
    Best Regards,

    Kaper

  6. #6
    Forum Contributor
    Join Date
    06-20-2021
    Location
    UK
    MS-Off Ver
    2016
    Posts
    126
    Quote Originally Posted by rorya View Post
    Do your weeks begin on Friday? If not, I'm not sure that function works.

    Begin Monday

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: Getting Week Number within month time frame: why 13 and -5?

    So tomorrow (same as today, yestarday and the day before yesterday) is 5th week of June, then Friday, Sat, Sun is 1st week of July, and starting Mon, July th is 2nd week of July.

    If so the function from first post is wrong. It shall read:

    Please Login or Register  to view this content.
    All the explanations why 13 and why for Sunday (not Thu in this case) it shall be 7 are still valid.

  8. #8
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Getting Week Number within month time frame: why 13 and -5?

    As an aside, you could do this with a formula and bypass the UDF:

    Please Login or Register  to view this content.
    WBD

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Count for Day of the Week and Time Frame
    By zjschmidt in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-11-2021, 07:21 PM
  2. Week number in month determined by 3/4 weekdays in week within said month
    By atearth in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2017, 02:36 PM
  3. Number of Samples in a Month time frame
    By lchee in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-03-2016, 03:48 AM
  4. [SOLVED] Calculate Week Range from Week Number, Month and Year?
    By A108A108 in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 05-07-2016, 03:11 PM
  5. Replies: 5
    Last Post: 09-01-2014, 03:55 PM
  6. Replies: 10
    Last Post: 01-22-2012, 10:05 AM
  7. How to calculate days of the week in a given time frame
    By fsoares22 in forum Excel General
    Replies: 2
    Last Post: 03-17-2011, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1