Finding the last Sunday of a month

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.

Thanks,
Peter

···

--
Posted via http://www.ruby-forum.com/.

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

Hmm I believe that
d = Date.new( now.year, 1, 31 )
d - d.wday
should do the trick
HTH
Robert

···

On 9/26/07, Peter Bailey <pbailey@bna.com> 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.

Thanks,
Peter
--
Posted via http://www.ruby-forum.com/\.

--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn

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
  
The last Sunday of that month can be got this way, I think:

require 'date'
now = DateTime.now
year = now.year
d = Date.new(now.year, 1, 31)
i = d.wday
last_sunday = Date.new(now.year, 1, (31-i))
puts last_sunday

The reference for dates is at:
http://corelib.rubyonrails.org/classes/Date.html

Cheers
mohit.

Not perfect, but something like this:

Cassady:~ yossef$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = Date.new(2007, 2, 1)
=> #<Date: 4908265/2,0,2299161>
irb(main):003:0> d.to_s
=> "2007-02-01"
irb(main):004:0> d -= 1
=> #<Date: 4908263/2,0,2299161>
irb(main):005:0> d.to_s
=> "2007-01-31"
irb(main):006:0> d -= d.wday
=> #<Date: 4908257/2,0,2299161>
irb(main):007:0> d.to_s
=> "2007-01-28"
irb(main):008:0> exit
Cassady:~ yossef$ cal 1 2007
    January 2007
S M Tu W Th F S
    1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

The "secret", as I see it, is getting the last day of the month and
subtracting that date's wday from it.

Note that I start by getting the first day of the next month and going
back one day. I'm not sure about a sure-fire way to always get the
last day of a month by going forward (because you can get errors
trying to hit the 31st of September), but going back one day from the
first of the next month should work just fine.

···

On Sep 26, 8:44 am, Peter Bailey <pbai...@bna.com> 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.

Thanks,
Peter
--
Posted viahttp://www.ruby-forum.com/.

--
-yossef

Just for fun:

y=`cal`[/\d+/]
d = `cal 1 #{y}`.scan(/^.{20}$/)[-1].
  split[-1].to_i

···

On Sep 26, 8:44 am, Peter Bailey <pbai...@bna.com> 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

I was at work and am getting into this one late, but there it is.

I would like to have something that handles the different month lengths,
and not just February. I checked to see what the value of a day was
this way:

p Time.gm(2007, 'feb', 2, 1, 1, 1) - Time.gm(2007, 'feb', 1, 1, 1, 1)

returns 86400 which is the number for exactly one day.

Therefore, I have this approach to get the day:

t = Time.gm(2007, 'feb', 1, 1, 1, 1) # to find for January
p (t - (one_day * t.wday)).day

:slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

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

···

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.

--

Jeremy Hinegardner jeremy@hinegardner.org

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

Thanks to all of you. Mohit's worked for me. It's quite ingenious,
actually, because it's simply subtracing the date of the week, which is
3 for Wednesday, from 31, the last day of the month. 31 - 3 = 28, so,
Sunday is the 28th. I love Ruby!

Thanks again,
Peter

···

--
Posted via http://www.ruby-forum.com/.

I think using -1 as the day of month gets you the last day of the
month.

irb(main):002:0> d = Date.new(2007, 1, -1)
=> #<Date: 4908263/2,0,2299161>
irb(main):003:0> d.to_s
=> "2007-01-31"
irb(main):004:0> (d - d.wday).to_s
=> "2007-01-28"

--Dale

···

On Sep 26, 9:00 am, Yossef Mendelssohn <ymen...@pobox.com> wrote:

On Sep 26, 8:44 am, Peter Bailey <pbai...@bna.com> 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.

> Thanks,
> Peter
> --
> Posted viahttp://www.ruby-forum.com/.

Not perfect, but something like this:

Cassady:~ yossef$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> d = Date.new(2007, 2, 1)
=> #<Date: 4908265/2,0,2299161>
irb(main):003:0> d.to_s
=> "2007-02-01"
irb(main):004:0> d -= 1
=> #<Date: 4908263/2,0,2299161>
irb(main):005:0> d.to_s
=> "2007-01-31"
irb(main):006:0> d -= d.wday
=> #<Date: 4908257/2,0,2299161>
irb(main):007:0> d.to_s
=> "2007-01-28"
irb(main):008:0> exit
Cassady:~ yossef$ cal 1 2007
    January 2007
S M Tu W Th F S
    1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

The "secret", as I see it, is getting the last day of the month and
subtracting that date's wday from it.

Note that I start by getting the first day of the next month and going
back one day. I'm not sure about a sure-fire way to always get the
last day of a month by going forward (because you can get errors
trying to hit the 31st of September), but going back one day from the
first of the next month should work just fine.

Yes, thanks Joseff. That's what worked for me. Subtracting the "weekday"
from the "monthday."

···

--
Posted via http://www.ruby-forum.com/.

Lloyd Linklater wrote:

t = Time.gm(2007, 'feb', 1, 1, 1, 1) # to find for January
p (t - (one_day * t.wday)).day

oops. one_day = 86400

···

--
Posted via http://www.ruby-forum.com/\.

Speaking of gems, there's also Runt:

http://runt.rubyforge.org/

Nathan Murray 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 Bowkett

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

Jeremy Hinegardner 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:

···

--
Posted via http://www.ruby-forum.com/\.

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

Regards,
Ammar

···

On Tue, Nov 9, 2010 at 10:24 AM, Rashmi P. <rrparida@yahoo.com> 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

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

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. <rrparida@yahoo.com> 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

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

···

--
Posted via http://www.ruby-forum.com/\.

Peter Bailey wrote:

Thanks to all of you. Mohit's worked for me. It's quite ingenious, actually, because it's simply subtracing the date of the week, which is 3 for Wednesday, from 31, the last day of the month. 31 - 3 = 28, so, Sunday is the 28th. I love Ruby!

Thanks again,
Peter

Actually, I think mine just reached your first but everyone had essentially the same suggestion. By the way, the suggestion about subtracting from the 1st of the next month is perhaps smarter! That way, you don't hv to worry about what the last day of the month is (31, 30, 28, 29?) I think it's really smart to just avoid all of that and go back from the start of the next month!

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

Hey, you're right! At least that seems to work.

Thanks!

(Note: Getting this from you is apparently easier than reading the
documentation for Date.new (really Date.civil), which says "m and d
can be negative, in which case they count backwards from the end of
the year and the end of the month respectively.")

···

On Sep 26, 9:10 am, Dale Martenson <dale.marten...@gmail.com> wrote:

On Sep 26, 9:00 am, Yossef Mendelssohn <ymen...@pobox.com> wrote:

> On Sep 26, 8:44 am, Peter Bailey <pbai...@bna.com> 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.

> > Thanks,
> > Peter
> > --
> > Posted viahttp://www.ruby-forum.com/.

> Not perfect, but something like this:

> Cassady:~ yossef$ irb
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> d = Date.new(2007, 2, 1)
> => #<Date: 4908265/2,0,2299161>
> irb(main):003:0> d.to_s
> => "2007-02-01"
> irb(main):004:0> d -= 1
> => #<Date: 4908263/2,0,2299161>
> irb(main):005:0> d.to_s
> => "2007-01-31"
> irb(main):006:0> d -= d.wday
> => #<Date: 4908257/2,0,2299161>
> irb(main):007:0> d.to_s
> => "2007-01-28"
> irb(main):008:0> exit
> Cassady:~ yossef$ cal 1 2007
> January 2007
> S M Tu W Th F S
> 1 2 3 4 5 6
> 7 8 9 10 11 12 13
> 14 15 16 17 18 19 20
> 21 22 23 24 25 26 27
> 28 29 30 31

> The "secret", as I see it, is getting the last day of the month and
> subtracting that date's wday from it.

> Note that I start by getting the first day of the next month and going
> back one day. I'm not sure about a sure-fire way to always get the
> last day of a month by going forward (because you can get errors
> trying to hit the 31st of September), but going back one day from the
> first of the next month should work just fine.

I think using -1 as the day of month gets you the last day of the
month.

irb(main):002:0> d = Date.new(2007, 1, -1)
=> #<Date: 4908263/2,0,2299161>
irb(main):003:0> d.to_s
=> "2007-01-31"
irb(main):004:0> (d - d.wday).to_s
=> "2007-01-28"

--Dale

--
-yossef