Skip to content

Instantly share code, notes, and snippets.

@hara-y-u
hara-y-u / week_index_in_month.rb
Created January 10, 2025 05:15
Calculate week index in month / Rubyで特定日が何番目の週か計算
require "date"
# Days from day one to first saturday are result in index 1
def week_index_in_month(date)
first_day_of_month = Date.new(date.year, date.month, 1)
((date.day + first_day_of_month.wday) / 7.0).ceil
end
# Days from day one to seven are result in index 1
def week_index_in_month(date)