From: Jeremy Hinegardner Date: 2007-09-27T13:16:51+09:00 Subject: Re: Finding the last Sunday of a month On Wed, Sep 26, 2007 at 10:44:42PM +0900, Peter Bailey wrote: > Hello, > I need to find the date for the last Sunday in January, for any year. I > need this for a budgetary script I'm trying to write. Using the > Date/Time module, I've done this so far. > > require 'date' > now = DateTime.now > year = now.year > d = Date.new(now.year, 1, 31) > puts d.wday > > yields: 3 > > This tells me that the last day of January is a Wednesday. But, I need > that last Sunday. Is there a method in 'date' that can give me the date > of the last Sunday? The last Sundays of each month are the boundaries > for my company's budget periods. Well this has been answered fully now, but just for fun, how about the fully gem driven method: % cat last_sun_jan.rb require 'rubygems' require 'chronic' require 'facet/times' require 'facet/time/to_date' last_sunday_in_jan = (Chronic.parse("1st Sunday in February", :context => :future) - 7.days).to_date puts last_sunday_in_jan % ruby last_sun_jan.rb 2008-01-27 enjoy, -jeremy -- ======================================================================== Jeremy Hinegardner jeremy@hinegardner.org