[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Calculate last day of month

sig_UVA

9/21/2006 7:00:00 PM

This is probably an easy one for somebody, but I couldn't figure it out
using the Date class in the documentation.



require 'date'


d = Date.new(2006, 9,16)


How do I return the date for the last day of this month (September in
this example)?



Thank you for your help!


-Hunter


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

19 Answers

Paul Lutus

9/21/2006 7:06:00 PM

0

Hunter Walker wrote:

> This is probably an easy one for somebody, but I couldn't figure it out
> using the Date class in the documentation.
>
> How do I return the date for the last day of this month (September in
> this example)?

Easy: Construct a date for the first day of the following month, then
subtract one day and read the resulting components.

Hard: construct a date for the first day of the target month, then add days
unitil the month number changes. Then go back one.


I think I know which option you'll choose. :)

--
Paul Lutus
http://www.ara...

Ara.T.Howard

9/21/2006 7:37:00 PM

0

sig_UVA

9/21/2006 7:50:00 PM

0



Very cool! That worked! Thank you!

-Hunter

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

Ceol

9/21/2006 8:14:00 PM

0

> d += 42 # warp into the next month

You need to add a month, not a fixed number of days, since you can't predict
how many days are in the next month:

irb(main):033:0> d = Date.new 2006, 1, 31
=> #<Date: 4907533/2,0,2299161>
irb(main):034:0> (d >> 1).to_s
=> "2006-02-28"


irb(main):039:0> d = Date.new 1996, 1, 31
=> #<Date: 4900227/2,0,2299161>
irb(main):040:0> (d >> 1).to_s
=> "1996-02-29"

- James Moore


Patrick Hurley

9/21/2006 8:20:00 PM

0

On 9/21/06, James Moore <banshee@banshee.com> wrote:
> > d += 42 # warp into the next month
>
> You need to add a month, not a fixed number of days, since you can't predict
> how many days are in the next month:
>
> irb(main):033:0> d = Date.new 2006, 1, 31
> => #<Date: 4907533/2,0,2299161>
> irb(main):034:0> (d >> 1).to_s
> => "2006-02-28"
>
>
> irb(main):039:0> d = Date.new 1996, 1, 31
> => #<Date: 4900227/2,0,2299161>
> irb(main):040:0> (d >> 1).to_s
> => "1996-02-29"
>
> - James Moore
>
>
>

Yeah that was my first thought too, but notice he is constructing a
new date at the beginning of the month. I am guessing he picked 42,
over 32 as it is just much better number.

pth

Hoppy

9/21/2006 8:40:00 PM

0

> How do I return the date for the last day of this month (September in
> this example)?

Looking for an alternate way: could this be a step in the right
direction?
(See http://chronic.ruby...)

irb(main):001:0> require 'chronic'
irb(main):011:0> Chronic.parse('last day of this month')
=> nil
Well... this was close.
:-)

Thomas Preymesser

9/21/2006 8:57:00 PM

0

On 21/09/06, Hunter Walker <walkerhunter@gmail.com> wrote:
> How do I return the date for the last day of this month (September in
> this example)?

Date.new((Date.today>>1).year,(Date.today>>1).month,1)-1)

-Thomas

Ara.T.Howard

9/21/2006 9:04:00 PM

0

Robert Klemme

9/21/2006 9:59:00 PM

0

ara.t.howard@noaa.gov wrote:
> On Fri, 22 Sep 2006, Patrick Hurley wrote:
>
>> Yeah that was my first thought too, but notice he is constructing a
>> new date at the beginning of the month. I am guessing he picked 42,
>> over 32 as it is just much better number.
>
> exactly ;-)
>
> the point is that it always lands into the next month.

Or the month after that - depending on where you start. :-)

robert

Ara.T.Howard

9/21/2006 10:10:00 PM

0