[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Finding the last Sunday of a month

Peter Bailey

9/26/2007 1:45:00 PM

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

25 Answers

Robert Dober

9/26/2007 1:58:00 PM

0

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

Hmm I believe that
d = Date.new( now.year, 1, 31 )
d - d.wday
should do the trick
HTH
Robert
> 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-....
>
>


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

Mohit Sindhwani

9/26/2007 1:58:00 PM

0

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

Cheers
mohit.






Yossef Mendelssohn

9/26/2007 2:00:00 PM

0

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

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.

--
-yossef


Peter Bailey

9/26/2007 2:07:00 PM

0

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

Dale Martenson

9/26/2007 2:10:00 PM

0

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

Mohit Sindhwani

9/26/2007 2:12:00 PM

0

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.



Peter Bailey

9/26/2007 2:12:00 PM

0

Yes, thanks Joseff. That's what worked for me. Subtracting the "weekday"
from the "monthday."
--
Posted via http://www.ruby-....

Peter Bailey

9/26/2007 2:27:00 PM

0

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


Point taken. You're right. Thanks.
--
Posted via http://www.ruby-....

Yossef Mendelssohn

9/26/2007 2:37:00 PM

0

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

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.")

--
-yossef


Peter Bailey

9/26/2007 2:53:00 PM

0

OK. Now, I've got that last Sunday's date in January. And, I've now
delineated all of the successive Sundays thereafter. Each of them is a
budgetary fencepost for me. So, can someone help me to determine what
budget period, out of all 13 of them, that today's date, or any day for
that matter, would fall into? I think that all of my budget dates below
are actually strings, so, they don't parse with dates as they are.

Thanks,
Peter

Here's what I have now. It works. I just need to see what today, or any
day, would fall into.

require 'date'
now = DateTime.now
year = now.year
d = Date.new(now.year, 2, 1)
d.to_s
d -= 1
d.to_s
d -= d.wday
i = d.wday
d.to_s
firstbudgetfence = d

t = Time.now
t = t.strftime("%Y-%m-%d")
budget1 = Date.new(now.year, 1, 1)
budget2 = firstbudgetfence
budget3 = firstbudgetfence + 28
budget4 = budget3 + 28
budget5 = budget4 + 28
budget6 = budget5 + 28
budget7 = budget6 + 28
budget8 = budget7 + 28
budget9 = budget8 + 28
budget10 = budget9 + 28
budget11 = budget10 + 28
budget12 = budget11 + 28
budget13 = budget12 + 28
--
Posted via http://www.ruby-....