To find 2nd Sunday of March

R

Rajaa Ramanathan

All,
I have used the following code to find the second Sunday of march.
lastdate_of_march = Date.new(current_year, 3, 31)
i = lastdate_of_march.wday
second_sunday = Date.new(current_year, 3, (31-i))

is there any simple method to find Second Sunday of March.
If any ! knows kindly help me..

Regards,
Rajaa
 
R

Rob Biedenharn

Come on, that's really not difficult. Start with the first day of
March and go from there. Knowing how many days there are in a week
should be sufficient. Note also, that you can use math operators on
Date instances (see "ri Date").

Cheers

robert

Think about it like this (assuming that you don't want to install the
chronic gem):

The second week is the 8th to the 14th of the month. Sunday is wday 0.

If the 14th is a Monday, which has a wday of 1, the second Sunday is
14-1=13th.

end_of_week_2 = Date.civil(Date.today.year, 3, 14)
second_sunday = end_of_week_2 - end_of_week_2.wday

Sunday is easiest with this method (because it is wday==0), but you
can generalize it to any day of the week.

-Rob

Rob Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
R

Reid Thompson

All,
I have used the following code to find the second Sunday of march.
lastdate_of_march = Date.new(current_year, 3, 31)
i = lastdate_of_march.wday
second_sunday = Date.new(current_year, 3, (31-i))

is there any simple method to find Second Sunday of March.
If any ! knows kindly help me..

Regards,
Rajaa
rthompso@raker>~
$ irb
irb(main):001:0> require 'chronic'
=> true
irb(main):002:0> Chronic.parse('2nd sunday in march', :context => :future)
=> Sun Mar 13 13:00:00 -0400 2011
irb(main):003:0> Chronic.parse('2nd sunday in march', :context => :past)
=> Sun Mar 14 13:00:00 -0400 2010
irb(main):004:0>
irb(main):005:0> Chronic.parse('2nd sunday in march')
=> Sun Mar 13 13:00:00 -0400 2011
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top