Finding the last Sunday of a month

caveat time!

If the first of the month is Sunday, my code does not work. Here is a
working version:

one_day = 86400
t = Time.gm(2008, ‘jun’, 1, 1, 1, 1)
if t.wday == 0
t -= one_day
end
p (t - (one_day * t.wday)).day

Someone else can golf it.

On Wed, Sep 26, 2007 at 10:44:42PM +0900, Peter B. wrote:

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

Speaking of gems, there’s also Runt:

http://runt.rubyforge.org/

Nathan M. used it in his Backup gem:

http://tech.natemurray.com/backup/

It contains code for autogenerating dates for the last (any weekday)
in the current month.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

On Sep 27, 2007, at 7:46 AM, Lloyd L. wrote:

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

That is really cool, Jeremy, except that the last Sunday in January is
the 28th, not the 27th. Other than that, cool stuff! :slight_smile:

Not in 2008, it’s the 27th then.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Jeremy H. wrote:

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

That is really cool, Jeremy, except that the last Sunday in January is
the 28th, not the 27th. Other than that, cool stuff! :slight_smile:

Hi,
Can any of you pls tell me, how to find the third(any) Monday(any
day) of a month. I can give input as " Third Monday January 2010" and
expecting an output as “2010/01/18” as an date object.

def getDate(Third, monday, January, 2010)

end

The above function should return me as “2010/01/18”.

Hope of a quick reply from you guys.

Regards
RRP

Rob B. wrote:

Not in 2008, it’s the 27th then.

OOOOOOOOOOOO!! Outstanding catch! That makes it extra cool! I missed
the changed year as I used this one for my testing. :slight_smile:

Hi Mohit,
Your code piece is not working for me. I have installed chronic
and facets successfully but it displays error for facet/times and
facet/time/to_date. Pls advise me how to sort out this error.

Error: No such file to load during compilation

Error: could not find a valid gem in any repository for gem
installation.

On Tue, Nov 9, 2010 at 10:24 AM, Rashmi P. [email protected] wrote:

Hi,
Can any of you pls tell me, how to find the third(any) Monday(any
day) of a month. I can give input as " Third Monday January 2010" and
expecting an output as “2010/01/18” as an date object.

def getDate(Third, monday, January, 2010)

end

Please don’t change the title of your post to ask a new, or the same,
question. It looks like a new post for those of us that interact with
the forum via email.

My suggestion for you to use chronic has been already been suggested.

Regards,
Ammar

On Tue, Nov 9, 2010 at 10:24 AM, Rashmi P. [email protected] wrote:

The above function should return me as “2010/01/18”.
Check out the chronic gem. http://chronic.rubyforge.org/

Regards,
Ammar

On Wed, Apr 06, 2011 at 10:56:34PM +0900, Thomas P. wrote:

you could use my ‘weekday’ gem:

Is there somewhere to get the source, read stats about it, et cetera,
without actually installing it via the gem command? RubyForge seems
like
the only option, judging by a cursory search – but a checkout doesn’t
yield any actual source, and it’s difficult to find useful information
on
RubyForge.

Rashmi P. wrote in post #960268:

Can any of you pls tell me, how to find the third(any) Monday(any
day) of a month.

Hello,

you could use my ‘weekday’ gem:

myDate = Weekday.third_monday(2010,1)

of course any combination of mondays … sunday, and first … fifth and
last is valid.

-Thomas