[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Days in month

Dan Fitzpatrick

4/25/2005 5:59:00 PM

Is there a method of Date or Time that gives the number of days in a
date's month or the last day of the month like:

d = Date.new(2005,4,20)
d.days_in_month #-> 30
#or
d.last_day_of_month #-> 30

Thanks,

Dan




6 Answers

dblack

4/25/2005 6:20:00 PM

0

jc

4/25/2005 6:21:00 PM

0

class Date
def days_in_month
((Date.new(year, month, 1) >> 1)-1).day
end
end

d = Date.new(2005,4,20)
puts d.days_in_month #-> 30

Dan Fitzpatrick

4/25/2005 7:18:00 PM

0

JC,

Thanks for the simple solution.

Dan


jc wrote:
> class Date
> def days_in_month
> ((Date.new(year, month, 1) >> 1)-1).day
> end
> end
>
> d = Date.new(2005,4,20)
> puts d.days_in_month #-> 30
>
>


Brian Buckley

4/25/2005 7:23:00 PM

0

!!! Yowser!

kennethkunz

4/25/2005 7:44:00 PM

0

Nice! Here's a slight variation that's even simpler:

class Date
def days_in_month
Date.civil(year, month, -1).day
end
end

Ara.T.Howard

4/28/2005 1:48:00 AM

0