Tuesday, May 26, 2009

Get the list of sundays in a month

require 'date'

class Date
def self.last_day_of_the_month yyyy, mm
d = new yyyy, mm
d += 42 # warp into the next month
new(d.year, d.month) - 1 # back off one day from first of that month
end

def self.print_sundays(d1, d2)
d1 +=1 while (d1.wday != 0)
d1.step(d2, 7) do |date|
puts "#{Date::MONTHNAMES[date.mon]} #{date.day}"
end
end
end

date = Date.today
month = date.strftime("%m").to_i
year = date.strftime("%Y").to_i

last_date = Date.last_day_of_the_month(year, month)
l_date = last_date.strftime("%d").to_i
Date.print_sundays(Date::civil(year, month, 1), Date::civil(year, month, l_date))

No comments: