Try'n'Go: The Last Date
Join the DZone community and get the full member experience.Join For Free
Extends Date class with a method that return the last Date of a month. Date#last_of_month takes either a Fixnum or a Time as argument.
require 'date' class Date def self.last_of_month( arg = Time.now ) year = ( arg.is_a? Fixnum ) ? Time.now.year : arg.year mon = ( arg.is_a? Fixnum ) ? arg : ( arg.mon rescue Time.now.mon ) raise ArgumentError unless mon.between?( 1, 12 ) begin; Date.new year, mon, mday ||= 31 rescue ArgumentError; mday -= 1; retry end end end
Opinions expressed by DZone contributors are their own.