Finding the last Sunday of a month

P

Peter Bailey

There are numerous optimizations possible...
* You could consider putting all the budget posts in an array and
iterating over the array using a smaller loop code.
* You could check what month it is and check only the budget posts
(month-1) and (month) and (month+1). If I'm not wrong, no more than 3
budget periods will fall in the same month - it's a bit convoluted
though :p
* If you are in fact creating the budget fenceposts as you go along, you
don't need to generate all 12.. just generate till you find the one you
are looking for.

Anyway, there are a few options - enjoy!

Cheers,
Mohit.
9/26/2007 | 11:38 PM.


Good suggestions all, Mohit. But, I just had to get this thing working
first, to get "the work out," so to speak. And, yes, when I have time, I
will look at what you suggest. Cheers.
 
J

Jeremy Hinegardner

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
 
L

Lloyd Linklater

Jeremy said:
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! :)
 
L

Lloyd Linklater

Rob said:
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. :)
 
R

Rashmi P.

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
 
R

Rashmi P.

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.
 
A

Ammar Ali

Hi,
=C2=A0 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".

Check out the chronic gem. http://chronic.rubyforge.org/

Regards,
Ammar
 
A

Ammar Ali

Hi,
=C2=A0 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
 
T

Thomas Preymesser

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top