Deal with different ways of counting weeks and weekdays in Ruby
Depending on where you live, different rules are used to determine the number of the week and a weekday. You have no chance whatsoever to get this right globally unless you make it your life's purpose. However, when you work for clients from Europe or the US, there are two dominantish standards you should know about. Each of these has subtle differences.
- This is adhered to by most European countries.
- Weeks start on Monday and end on Sunday.
- To get the number of the weekday in Ruby as defined by ISO 8601, use Date#cwday, which returns values in
1..7(mind the range difference to Date#wday).
- The first week of the year is the week with January 4th in it. All days in this week belong to the year. This means that January 1st, 2nd and 3rd may or may not be part of the previous year's last calendar week. Also December 29th, 30th and 31st may or may not be part of the upcoming year's first calendar week.
- To get the calendar week in Ruby as defined by ISO 8601, use Date#cweek, which returns values in
USA and Canada
- Not defined in a standard.
- Weeks start on Sunday and end on Saturday.
- To get the number of the weekday in Ruby as used in the USA, use Date#wday, which returns values in
0..6(mind the range difference to Date#cwday)
- January 1st is part of the first week of the year. I am not sure which year December days in the same week belong to, I believe the week actually gets split into two calendar weeks in two different years (please change this note if you know).
- AFAIK there is no Ruby method to get the calendar week of a
Dateas used in the USA.
Author of this card:
- About this deck:
- We are makandra and do test-driven, agile Ruby on Rails software development.