week_of_month gem looks like it might be a bit overkill. This implementation uses many array sections and an Array.include? check Array.include? .
Instead, here is a module that you can mix with Date and Time to get the desired behavior.
require "active_support/core_ext/date" require "active_support/core_ext/time" module WeekCalculator def week_of_year(mondays = false) # Use %U for weeks starting on Sunday # Use %W for weeks starting on Monday strftime(mondays ? "%W" : "%U").to_i + 1 end def week_of_month(mondays = false) week_of_year(mondays) - beginning_of_month.week_of_year(mondays) + 1 end end class Date include WeekCalculator end class Time include WeekCalculator end
Date.new(2014, 1, 1).week_of_year # => 1 Date.new(2014, 1, 1).week_of_month # => 1 Date.new(2014, 7, 1).week_of_year # => 27 Date.new(2014, 7, 1).week_of_month # => 1 Date.new(2014, 7, 27).week_of_year # => 31 Date.new(2014, 7, 27).week_of_month # => 5 Date.new(2014, 7, 27).week_of_year(:monday) # => 30 Date.new(2014, 7, 27).week_of_month(:monday) # => 4
source share