# Is it more proper to bias floor or ceil when calculating the week of the year?

As per the title, I’m trying to calculate the week of the year. This should be simple by using common sense to rule out a “0th” week but I’m putting bias towards the whole number because of the decimal. There are 52.1429 weeks in a year and 0.1429 is closer to 52 than it is to 53.

Floor results in a 0th week of 6 days and a 52nd week of 2 days or 3 in a leap year. Ceil results in a 53rd week of 1 day or 2 in a leap year, which is where that 0.1429 steps into play.

Trials:

``````-- Subtract 1 duplicate print from the last week
-- of each loop to find days in a 365-day year
for i = 1, 366 do print(math.floor(i/7)) end
for i = 1, 366 do print(math.ceil(i/7)) end
``````

Couldn’t find anything about what’s best to do. I found differing calculations and functions, some directly working with the 52.1429 number rather than rounding to find the week of the year, so there’s obviously a few ways to go about finding the week of the year. In my case, using rounding, I don’t know which one to bias and why.

2 Likes

Well it would be best to round down since if you round up you end up skipping 5 days, with rounding down you could just have the 52nd week with an extra day
Also could i have some context as to what this is for? my example might not be valid in some situations

No days are skipped regardless of if I round up or down. There are still 365 days in a year or 366 if rounded up. You can check this by testing the calculations. What I’m concerned about is that choosing to round up or down influences the number of weeks in the year.

Some weeks have less days than 7 because of the way the math works in both scenarios. There are 54.1429 weeks in a year which encompass all 365/366 days. No days are lost. The week number, however, can change based on how you round. I want to know which rounding to bias to get the best number here, or if I should use a different method.

Oh i believe i understand now

Well the simple answer would be just round down as the week number wont change but lets not do that
In my opinion you should probably make a function that calculates the amount of days in a week
I’ll see if i can give you an example function
But for now rounding down would be your best option as you would lose 1 day instead of gain 5 days

You don’t. This doesn’t answer my question and isn’t helpful at all. Appreciate the attempt though. It would greatly help if you understood what the actual thread is asking, the concept work going on here and the math before answering again though. I’m looking for knowledgeable answers, not prospective attempts. Thanks!

I think this might be what you’re looking for:

``````local week = tonumber(os.date("%W"))
or
local week = tonumber(os.date("%U"))
``````

https://developer.roblox.com/en-us/api-reference/lua-docs/os

2 Likes

Bruh, when’d they add support on os.date for week of the year? Either that’s always been a thing but it’s just never been documented, or that’s new, or I’m plain stupid. What.

Thank you, this helps a lot. I can literally just use this instead of doing any math. Can’t believe I didn’t follow my own advice to search first and check if there was any updates to documentation.

5 Likes