Ruby Tuesday – Date#cweek

Today’s Ruby Tuesday is on Date#cweek.

Date#cweek returns a integer value representing the week of the year that the date falls on. The return value is an integer between 1 and 53.

Date.new(2015, 5, 3).cweek
# => 18
Date.new(2015, 5, 4).cweek
# => 19

If we use this week as an example, we can see that a week starts on Monday (the 4th of May), where the Sunday before (May 3rd) was the previous week.

We see that January 1st falls on the first week of the year, no surprise there, and that the 31st of December for 2015, is on the 53rd week of the year.

Date.new(2015, 1, 1).cweek
# => 1
Date.new(2015, 12, 31).cweek
# => 53

Having a 53rd week of the year sounds suprising at first, because everyone talks about 52 weeks in a year, until you realize that sometimes December 31st sometimes falls at the very beginning of a week, causing it to be the 53rd week, since it is only a partial week.

–Proctor

Leave a Reply

Your email address will not be published. Required fields are marked *