From: ara.t.howard@... Date: 2006-09-22T04:36:37+09:00 Subject: Re: Calculate last day of month On Fri, 22 Sep 2006, Paul Lutus wrote: > Hunter Walker wrote: > >> This is probably an easy one for somebody, but I couldn't figure it out >> using the Date class in the documentation. >> >> How do I return the date for the last day of this month (September in this >> example)? > > Easy: Construct a date for the first day of the following month, then > subtract one day and read the resulting components. > > Hard: construct a date for the first day of the target month, then add days > unitil the month number changes. Then go back one. > > I think I know which option you'll choose. :) except you have to handle december specially - else you'll end up with the with the wrong year. easiest is probably: harp:~ > cat a.rb 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 end puts Date.last_day_of_the_month(2006, 9) puts Date.last_day_of_the_month(2006, 12) puts Date.last_day_of_the_month(2007, 2) harp:~ > ruby a.rb 2006-09-30 2006-12-31 2007-02-28 -a -- in order to be effective truth must penetrate like an arrow - and that is likely to hurt. -- wei wu wei