Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

week number of the year, kinda

Tags:

date

time

ruby

Preferably in ruby, but the logic would be good enough...

I need the week number of the year given that the week is non-standard. So, say you define a week as Saturday -> Friday. Then, given a date, which week number (1-52) is it?

strftime has %U:

> Time.now.strftime('%U') > => "28" 

...but that of course assumes a standard Sunday -> Saturday week.

like image 471
jsharpe Avatar asked Jul 16 '11 22:07

jsharpe


People also ask

What week of the year are we in 2022?

Week 42. Week 42 is from Monday, October 17, 2022 until (and including) Sunday, October 23, 2022. The highest week number in a year is either 52 or 53. 2022 has 52 weeks.

What is a week of the year?

The weeks of the year in a Gregorian calendar are numbered from week 1 to week 52 or 53, depending on several varying factors. Most years have 52 weeks, but if the year starts on a Thursday or is a leap year that starts on a Wednesday, that particular year will have 53 numbered weeks.

Which week are we now?

The calendar week for today is 42. Takes from Monday, 17.10. 2022 to Sunday, 23.10.

How do you calculate the week number in a year?

The week number is the count of weeks that have passed in the year up to the end of the current week. For example, if it's the second week of the year, the week number is 2. The week where Jan 1 falls counts as the first week for the following year.


1 Answers

Use %W instead of %U, it uses Monday as the first day of the week.

Time.now.strftime('%W') 
like image 97
gioele Avatar answered Sep 24 '22 08:09

gioele